473,698 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

renaming 'references' to functions can give recursive problems

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 "recursivemetho ds.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!!!
--------------------------------------------------------------------------

Jul 18 '05 #1
9 2072
see the topic:
adding new functionality to a function non-intrusively! and decorators
if you want to add functionality to a function!

Jul 18 '05 #2
Op 2005-02-16, peter schreef <Pe************ *****@gmail.com >:
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
Jul 18 '05 #3
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 <Pe************ *****@gmail.com >:
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


Jul 18 '05 #4
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=f A): .... 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=f A): .... 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
Jul 18 '05 #5
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

Jul 18 '05 #6
"peter" <Pe************ *****@gmail.com > 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>

Jul 18 '05 #7
brain reset and understood

thx a lot for all your answers

Peter

Jul 18 '05 #8
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
Jul 18 '05 #9
peter wrote:
brain reset and understood

thx a lot for all your answers

Peter

Now that you've got reset, you might want to consider an alternative solution:

def fA(input):
return input

oldfA = fA # Hold a reference to the the old function

def newFA(input):
"Do something new"
return oldfA(input)

fA = newFA
The advantage of this is that you don't need to change the function newfA at all
when you're ready to rename it.

Michael

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2011
by: Kaptain524 | last post by:
Hello, I am using PHP 5.0.4 with Apache 2, on WinXP Pro. This behavior appears to be fundamental however, and should not be affected by platform. It would seem that there is some kind of bug in the process that creates the reference when it is being assigned to an array element within itself. If it is already referenced, it just assigns the existing reference and avoids the problem.
5
3340
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
3
5887
by: SAM | last post by:
Hi everyone, I consider myself a very competent programmer when it comes to actual programming and analyzing the business that I'm modelling. I am now crossing into what I would consider Access admin/config territory, an area where I generally suck at. Here is my problem. I do all my programming on my laptop before giving
7
6128
by: Aloo | last post by:
Dear friends, If we declare a recursive function as 'inline' then does it actually convert to an iterative form during compilation ( the executable code is iterative)? Is it possible ? Please reply.
6
3112
by: jeniffer | last post by:
Please give an example of 2 simple mutually recursive functions in C .
8
2848
by: BillCo | last post by:
I'm updating a legacy app with table naming that makes baby jesus cry. It's a bit of a spider web though... no telling when and where the tables will be called by name. So I wrote this for renaming tables (I guess it would work for queries also). It changes the table name, changes the table name, queries that refer to it, form and report control sources, combo and list box row sources etc. Anyone inspired enough to figure out how to make...
23
1956
by: Steven D'Aprano | last post by:
I defined a nested function: def foo(): def bar(): return "bar" return "foo " + bar() which works. Knowing how Python loves namespaces, I thought I could do this:
41
3360
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions are that no local variable or global variable should be used.I have to use recursive functions.
0
939
by: Fabian von Romberg | last post by:
Hi, I have a small project that is compiled into one single assembly and obfuscated with Dotfuscator. When I test it I get error due to references problems (invalid namespaces, function names, etc). I know I can fix this by using the exceptions on the Dotfuscator settings. My question is if there is a way that my aspx files get fixed their references as well based on the renamings that is generated by Dotfuscator? Dotfuscator...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.