Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28992

Re: avoid the redefinition of a function

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'calls.': 0.07; 'try:': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'func': 0.09; 'sep': 0.09; 'def': 0.10; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'instance:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'reasonably': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'examples': 0.18; '>>>': 0.18; 'input': 0.18; 'stick': 0.22; 'example': 0.23; 'programming': 0.23; 'this:': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:[ 10': 0.26; 'select': 0.26; 'dictionary': 0.29; 'index,': 0.29; 'received:192.168.1.3': 0.29; 'selecting': 0.29; 'style.': 0.29; 'install': 0.29; 'function': 0.30; 'point': 0.31; 'could': 0.32; 'print': 0.32; '+0200,': 0.33; 'damage': 0.33; 'skip:j 20': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'add': 0.36; 'except': 0.36; 'skip:{ 10': 0.36; 'bad': 0.37; 'option': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'easy': 0.60; 'first': 0.61; 'more': 0.63; 'decided': 0.65; 'header:Reply-To:1': 0.68; '(5)': 0.71; 'reply-to:no real name:2**0': 0.72; 'opt': 0.78; 'descriptive': 0.84; 'reply-to:addr:python.org': 0.84; 'cause,': 0.93; 'poorly': 0.93
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=W6e6pGqk c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=AAvI7MrX_rgA:10 a=xRYOBlbL21QA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=OnyIV9Ab-_8A:10 a=I-dbhnWOaWboWtRh6IoA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Wed, 12 Sep 2012 19:33:12 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: avoid the redefinition of a function
References <CAOuJsMnLf4P9p_rZ4p1W-SndkSOV2WK_v5j1AET085LZ6bdavQ@mail.gmail.com> <5050938F.7030105@gmail.com> <mailman.572.1347469027.27098.python-list@python.org> <7944s.27167$CU7.24982@fx02.am4>
In-Reply-To <7944s.27167$CU7.24982@fx02.am4>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.575.1347474795.27098.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347474795 news.xs4all.nl 6939 [2001:888:2000:d::a6]:33712
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28992

Show key headers only | View raw


On 12/09/2012 19:04, Alister wrote:
> On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:
>
>>> For example:
>>>
>>> def install_java():
>>>    pass
>>>
>>> def install_tomcat():
>>>    pass
>>
>> Thanks for the answers. I decided to use numbers in the name of the
>> functions to facilitate function calls. Now if you have this menu option
>> for instance:
>>
>> (5) install mc
>>
>> You can type just "5" as user input and step_5() is called
>> automatically. If I use descriptive names like install_java() then
>> selecting a menu point would be more difficult. And I don't want users
>> to type "java", I want to stick to simple numbers.
>>
>> Laszlo
>
> No No NO!
> you cant just pass user input to system calls without validating it first
> (google sql injection for examples of the damage unsanitised input can
> cause, it is not just as SQL problem)
>
> it is just as easy so select a reasonably named function as a bad one
>
> option=raw_input('select your option :')
>
> if option =="1": install_java()
> if option =="2": install_other()
>
> alternatively you cold add your functions into a dictionary an call them
> from that
>
> opts={'1':install java,'2':install_other}
>
> option=raw_input('select your option :')
> opts[option]
>
> Poorly named functions are a major example of poor programming style.
>
> one of the fundamental pillars for python is readability!
>
Or you could do this:


def install_java():
     "Install Java"
     print "Installing Java"

def install_tomcat():
     "Install Tomcat"
     print "Installing Tomcat"

menu = [install_java, install_tomcat]

for index, func in enumerate(menu, start=1):
     print "{0}) {1}".format(index, func.__doc__)

option = raw_input("Select your option : ")

try:
     opt = int(option)
except ValueError:
     print "Not a valid option"
else:
     if 1 <= opt < len(menu):
         menu[opt - 1]()
     else:
         print "Not a valid option"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: avoid the redefinition of a function Jabba Laci <jabba.laci@gmail.com> - 2012-09-12 18:56 +0200
  Re: avoid the redefinition of a function Alister <alister.ware@ntlworld.com> - 2012-09-12 18:04 +0000
    Re: avoid the redefinition of a function MRAB <python@mrabarnett.plus.com> - 2012-09-12 19:33 +0100
    Re: avoid the redefinition of a function D'Arcy Cain <darcy@druid.net> - 2012-09-12 17:49 -0400
    Re: avoid the redefinition of a function Peter Otten <__peter__@web.de> - 2012-09-13 10:23 +0200
      Re: avoid the redefinition of a function Alister <alister.ware@ntlworld.com> - 2012-09-13 17:59 +0000

csiph-web