Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: avoid the redefinition of a function

Reply
Thread Tools

Re: avoid the redefinition of a function

 
 
Dave Angel
Guest
Posts: n/a
 
      09-13-2012
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: avoid the redefinition of a function Jabba Laci Python 3 09-13-2012 08:23 AM
Re: avoid the redefinition of a function Tim Chase Python 0 09-12-2012 05:34 PM
Re: avoid the redefinition of a function Michael Torrie Python 0 09-12-2012 01:52 PM
Re: avoid the redefinition of a function D'Arcy Cain Python 0 09-12-2012 01:51 PM
avoid the redefinition of a function Jabba Laci Python 2 09-12-2012 01:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57