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

 
 
Michael Torrie
Guest
Posts: n/a
 
      09-12-2012
On 09/12/2012 06:56 AM, Jabba Laci wrote:
> I have an installer script that contains lots of little functions. It
> has an interactive menu and the corresponding function is called. Over
> time it grew long and when I want to add a new function, I should give
> a unique name to that function. However, Python allows the
> redefinition of functions:
>
> #!/usr/bin/env python
>
> def step_1():
> print 1
>
> def step_1():
> print 2
>
> step_1()
>
> This will call the 2nd function. Now my functions are called step_ID
> (like step_27(), step_28(), etc.). How to avoid the danger of
> redefinition? Now, when I write a new function, I search for its name
> to see if it's unique but there must be a better way.


I don't understand the other poster's suggestion to your problem. I
have looked at your script and think I understand how you are using
them. Keep in mind that functions in python are just objects. So
rather than define them as step_##() and then search the globals list
for them, why not just define them as descriptive functions (for
example, "install_java") and then put them into a list.

For example:

def install_java():
pass

def install_tomcat():
pass


steps = [install_java, install_tomcat, etc]
# or steps.append(install_java) if steps already has stuff in it, etc

# then:
for step in steps:
step()


 
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 Terry Reedy Python 0 09-12-2012 08:37 PM
Re: avoid the redefinition of a function Tim Chase Python 0 09-12-2012 05:34 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