renaming 'references' to functions can give recursive problems

peter Peter.Vandersteegen at gmail.com
Wed Feb 16 09:54:05 EST 2005


Hello all,

Recently I've started to refactor my code ...(I'm using python 2.3.4)
I tried to add extra functionality to old functions non-intrusively.
When I used a construct, which involves renaming functions etc... I
came across some recursive problems.  (a basic construct can be found
under the section BASIC CODE)

These problems do not occur when renaming objects.  (see section EXTRA
CODE)
My question now is:
I do not know the underlying idea of functions.  Is this the way they
should behave? Or should they work the same way as objects do?
(My preferences goes to this last option)

BASIC CODE:
---------------------------------------------------------------------------
def fA(input): # starting point: function named fA
    return input

def newFA(input): # new function with added functionality
    #does something extra with a!
    return fA(input)
fA = newFA
     # this should allow to add functionality without
     # breaking older code which uses the name fA!
fA() # execute fA()
----------------------------------------------------------------------------
problem: you get...
File "recursivemethods.py", line 7, in newFA
    return fA(input)
RuntimeError: maximum recursion depth exceeded

when using objects this problems does not occur...
EXTRA CODE:
----------------------------------------------------------------------------
PYTHON-CODE                   # REMARKS
referenceA = SomeObject()     #  referenceA -> SomeObject()
                              #       references to the memory space of
                              #              some object
referenceB = referenceA       #  referenceB -> SomeObject()
                              #       is also a reference to ...
referenceA = referenceB       #  referenceA references to SomeObject()

# now for functions!
fA = function                 #  fA references to memory space
                              #  of a function

def newFA(input):             #  newFA references to a function
   return fA(input)           #  which holds a reference to fA.
                  #  Notice the important difference with objects!
                  #  newFA holds a reference to the reference fA
                  #  When using object you reference to
                  #  the memory space of fA!!!
fA = newFA
fA()              # recursive problems!!!
--------------------------------------------------------------------------




More information about the Python-list mailing list