avoid the redefinition of a function

Discussion in 'Python' started by Jabba Laci, Sep 12, 2012.

  1. Jabba Laci

    Jabba Laci Guest

    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.
     
    Jabba Laci, Sep 12, 2012
    #1
    1. Advertisements

  2. On Wednesday, 12 September 2012 18:26:36 UTC+5:30, Jabba Laci wrote:
    > 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. :)
    ---
    Bragging rights:SO account suspended
    py2c, a Python to *pure* C/C++ translator, is my project (I am the author)
    http://code.google.com/p/py2c/
     
    Ramchandra Apte, Sep 12, 2012
    #2
    1. Advertisements

  3. On Wednesday, 12 September 2012 18:26:36 UTC+5:30, Jabba Laci wrote:
    > 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. :)
    ---
    Bragging rights:SO account suspended
    py2c, a Python to *pure* C/C++ translator, is my project (I am the author)
    http://code.google.com/p/py2c/
     
    Ramchandra Apte, Sep 12, 2012
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Attila Feher

    Template redefinition problem

    Attila Feher, Sep 22, 2003, in forum: C++
    Replies:
    5
    Views:
    2,502
    tom_usenet
    Sep 22, 2003
  2. A
    Replies:
    1
    Views:
    696
    Josephine Schafer
    Oct 22, 2003
  3. Alexander Malkis
    Replies:
    8
    Views:
    830
    Alexander Malkis
    Apr 14, 2004
  4. Gernot Frisch

    redefinition, different types

    Gernot Frisch, Sep 13, 2004, in forum: C++
    Replies:
    13
    Views:
    2,452
    Jerry Coffin
    Sep 14, 2004
  5. Roger23
    Replies:
    2
    Views:
    1,219
    Roger23
    Oct 12, 2006
  6. aspineux
    Replies:
    5
    Views:
    686
    Paddy
    Mar 30, 2007
  7. lallous

    function redefinition and the standard

    lallous, Jan 26, 2004, in forum: Javascript
    Replies:
    2
    Views:
    170
    Lasse Reichstein Nielsen
    Jan 27, 2004
  8. Jabba Laci
    Replies:
    3
    Views:
    283
    Peter Otten
    Sep 13, 2012
Loading...