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


Groups > comp.lang.python > #29006

Re: avoid the redefinition of a function

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'calls.': 0.07; 'function,': 0.07; 'indirectly': 0.09; 'cc:addr:python-list': 0.10; 'useful,': 0.13; 'instance:': 0.16; 'item[0]': 0.16; 'sparks': 0.16; 'wrote:': 0.17; 'pointed': 0.17; 'input': 0.18; 'code.': 0.20; 'define': 0.20; 'together.': 0.21; 'stick': 0.22; 'tells': 0.22; 'defined': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'checking.': 0.29; 'index,': 0.29; 'routine': 0.29; 'selecting': 0.29; 'install': 0.29; 'function': 0.30; 'error': 0.30; 'point': 0.31; 'print': 0.32; 'choices': 0.33; 'function.': 0.33; 'like:': 0.33; 'problem': 0.33; 'anyone': 0.33; 'thanks': 0.34; 'list': 0.35; 'pm,': 0.35; 'received:192.168.0': 0.35; 'something': 0.35; 'but': 0.36; 'option': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'real': 0.61; 'more': 0.63; 'decided': 0.65; 'header:Reply-To:1': 0.68; '(5)': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'choices.': 0.84; 'descriptive': 0.84; 'received:74.208.4.194': 0.84; 'ideas.': 0.91
Date Wed, 12 Sep 2012 20:36:49 -0400
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To Jabba Laci <jabba.laci@gmail.com>
Subject Re: avoid the redefinition of a function
References <CAOuJsMnLf4P9p_rZ4p1W-SndkSOV2WK_v5j1AET085LZ6bdavQ@mail.gmail.com> <5050938F.7030105@gmail.com> <CAOuJsMm6uANJNSRD=Qe70sBYWnnX55yF9dqxQTCcbx_nSfZGBg@mail.gmail.com>
In-Reply-To <CAOuJsMm6uANJNSRD=Qe70sBYWnnX55yF9dqxQTCcbx_nSfZGBg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:9XycfPbMdJDHyYddFCeqMRMhj4mo3PpuIUDool1fdMw fy84qKQqpAGtUeJnBkHsN16JejzMyfsTvpIkbgvdDn38PHeQOT C7N726Zd5e4nnAfF37B+SdipQX+X40d4hnugQ31du4RcGyDMWr +iutYv9Bf3+6qkAJcCoa8XM8PI3PXfOhOsAbj6XU1QbJ68C2Mi HKO95qaZPFviI9I77O0o7JSde68sFC/WpdvmNnty/kx3hly+Js Pr/uTV0MSMcYcTxcFuvKxAzR1Bj3mFP9P0gVjevI8hHFx7o+8m 5vDcoNU5Myh0+T8FxNS2k+NwEnvNG8L6O20YdOSrVjQN60VAg= =
Cc Python mailing list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.585.1347496613.27098.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347496613 news.xs4all.nl 6842 [2001:888:2000:d::a6]:33765
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29006

Show key headers only | View raw


On 09/12/2012 12:56 PM, Jabba Laci wrote:
> 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

Many of the other answers were interesting and useful, but I don't think
anyone else pointed out the real problem with your code.

If you have a bunch of functions defined in one place, and a bunch of
choices for the user defined in another, you need to make one place
which maps them all together.  That way, when you define a new function,
you also define the part of the menu that tells the user what to type.

For example, you might make a list like the following:

CHOICES = [("install mc", install_mc),
                    ("install java", install_java),
                           ....etc....
                   ]

And your input routine will be something like:
           for  index, item in iterate(CHOICES):
                  print "(", index, ")", item[0]
           choice = int(raw_input())
           function = CHOICES[choice][1]

This isn't exactly the way i'd do it, and of course it's missing all
kinds of error checking.  But the point is that adding a new function to
the menu only requires one place to "know" the number of the function. 
And the number is only indirectly known, as the location in the CHOICES.

I hope this sparks some ideas.


-- 

DaveA

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


Thread

Re: avoid the redefinition of a function Dave Angel <d@davea.name> - 2012-09-12 20:36 -0400

csiph-web