avoid the redefinition of a function

J

Jabba Laci

Hi,

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.

Thanks,

Laszlo
P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
you are interested. It's made for Ubuntu.
 
R

Ramchandra Apte

Hi,



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.



Thanks,



Laszlo

P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if

you are interested. It's made for Ubuntu.

Use a code checker such as PyLint (http://www.logilab.org/857 or pylint package).
Better idea:
I *strongly* recommend to never use names such as step_12. Use descriptive names and the problem will not occur.
Your project looks interesting. I can contribute. :)
 
R

Ramchandra Apte

Hi,



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.



Thanks,



Laszlo

P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if

you are interested. It's made for Ubuntu.

Use a code checker such as PyLint (http://www.logilab.org/857 or pylint package).
Better idea:
I *strongly* recommend to never use names such as step_12. Use descriptive names and the problem will not occur.
Your project looks interesting. I can contribute. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top