renaming 'references' to functions can give recursive problems

2005-02-16 Thread peter
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!!!
--

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread peter
see the topic:
 adding new functionality to a function non-intrusively! and decorators
if you want to add functionality to a function!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread Antoon Pardon
Op 2005-02-16, peter schreef [EMAIL PROTECTED]:
 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()

Try this:

def fA(input):
  return input


def newFA(input, f= fA):
  return f(input)

fA = newFA

-- 
Antoon Pardon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread peter
Hello, nice solution:
but it puzzles me :)

can anyone tell me why
---correct solution
def fA(input):
  return input

def newFA(input, f= fA):
   return f(input)

fA = newFA

is correct and:
-infinite loop-

def fA(input):
  return input

def newFA(input):
   return fA(input)

fA = newFA

gives an infinite recursive loop?

kind regards

Peter

Antoon Pardon wrote:
 Op 2005-02-16, peter schreef [EMAIL PROTECTED]:
  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()

 Try this:

 def fA(input):
   return input


 def newFA(input, f= fA):
   return f(input)
 
 fA = newFA
 
 -- 
 Antoon Pardon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread Satchidanand Haridas
peter wrote:
Hello, nice solution:
but it puzzles me :)
can anyone tell me why
---correct solution
def fA(input):
 return input
def newFA(input, f= fA):
  return f(input)
fA = newFA
is correct and:
 

 def fA(input):
... print inside fA
... return input
...
 def newFA(input,f=fA):
... print inside newFA
... return f(input)
...
 fA = newFA
 fA(2)
inside newFA
inside fA
2
while:
-infinite loop-
def fA(input):
 return input
def newFA(input):
  return fA(input)
fA = newFA
gives an infinite recursive loop?
 

 def fA(input):
... print inside fA
... return input
...
 def newFA(input):
... print inside newFA
... return fA(input)
...
 fA = newFA
 fA(2)
inside newFA
inside newFA
inside newFA
inside newFA
What is happening is that when you call fA (inside newFA) in the second 
case, you are calling newFA because fA is pointing to newFA (hope that 
made sense ;-)). So it was recursive. While in the former case you 
called f, which pointed to fA, but not to newFA. Probably the following 
will make it clearer:

 def fA(input):
... print inside fA
... return input
...
 def newFA(input,f=fA):
... print inside newFA
... print f is pointing to: ,f
... return f(input)
...
 fA = newFA
 fA(2)
inside newFA
f is pointing to:  function fA at 0x43123374
inside fA
2
 fA
function newFA at 0x43194064
 newFA
function newFA at 0x43194064
Thus f and fA do not point to the same function object when you execute 
the statement fa(2). This f is called once and terminates.

thanks,
Satchit
--
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread Michael Spencer
peter wrote:
Hello, nice solution:
but it puzzles me :)
can anyone tell me why
---correct solution
def fA(input):
  return input
def newFA(input, f= fA):
   return f(input)
fA = newFA
is correct and:
-infinite loop-
def fA(input):
  return input
def newFA(input):
   return fA(input)
In newFA, fA is not bound until you call newFA.  By which time you've re-bound 
fA to newFA, causing the recursion.  In the 'correct' solution above, f is bound 
to the original fA function at the time the def fA statement is executed, which 
is what you want.

fA = newFA
gives an infinite recursive loop?
kind regards
Peter
Regards
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread Fredrik Lundh
peter [EMAIL PROTECTED] wrote
 
 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

nope.  it refers to a function object.  function objects are no different
from ordinary objects.

 def newFA(input): #  newFA references to a function

not yet.

   return fA(input)   #  which holds a reference to fA.

nope.  the function does not hold a reference to fA.  fA is a global,
and will be resolved at runtime.

  #  Notice the important difference with objects!
  #  newFA holds a reference to the reference fA

no, it doesn't hold a reference to the reference.  it contains the name
fA, and will look for that when you call it.

  #  When using object you reference to
  #  the memory space of fA!!!

you're confused.  resetting your brain and reading the documentation
again might help:

http://docs.python.org/ref/objects.html
http://docs.python.org/ref/naming.html

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread peter
brain reset and understood 

thx a lot for all your answers

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: renaming 'references' to functions can give recursive problems

2005-02-16 Thread Jeff Shannon
peter wrote:
Hello, nice solution:
but it puzzles me :)
can anyone tell me why
---correct solution
def fA(input):
  return input
def newFA(input, f= fA):
   return f(input)
This saves a reference to the original function in the new function's 
default argument.

-infinite loop-
def fA(input):
  return input
def newFA(input):
   return fA(input)
This does not save any reference to the original function; it simply 
does a run-time lookup of the name, and uses whatever object is 
currently bound to that name.  Since you later rebind the name to this 
new function, it's simply calling itself.

Jeff Shannon
Technician/Programmer
Credit International
--
http://mail.python.org/mailman/listinfo/python-list