Function attributes
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Wed Feb 10 10:33:34 EST 2010
On Wed, 10 Feb 2010 10:08:47 -0500, John Posner wrote:
>> This won't work correctly, because old_f still tries to refer to itself
>> under the name "f", and things break very quickly.
>
> They didn't break immediately for me -- what am I missing?:
The original function f doesn't try to refer to itself in any way. With
no recursive call or access, it doesn't matter what f is named.
See this example instead:
>>> def f(x):
... if x < 0:
... print "original"
... return f(-x)
... else:
... return x+1
...
>>> f(2)
3
>>> f(-2)
original
3
>>>
>>> old_f = f
>>> def f(x):
... if x > 0:
... return old_f(-x)
... else:
... return x
...
>>> f(-1)
-1
>>> f(1)
original
original
original
original
original
original
[...]
File "<stdin>", line 3, in f
File "<stdin>", line 4, in f
File "<stdin>", line 3, in f
RuntimeError: maximum recursion depth exceeded
--
Steven
More information about the Python-list
mailing list