1

Given the following code, i cant understand why f and fib behaves differently. the example taken from Barkley cs61a course

def fib(n):
    if n==1 or n==0:
        return n
    else:
        return fib(n-1) + fib(n-2)

def decor(f):
    def counted(*args):
        counted.call_count += 1
        return f(*args)
    counted.call_count = 0
    return counted

when i load the code into the interpreter i got this output:

>>> fib(6)
8
>>> f = decor(fib)
>>> fib = decor(fib)
>>> # f and fib are both vars that represents a decorated fib function

>>> f(6)
8
>>> f.call_count # why 1 ???
1
>>> 
>>> fib(6)
8
>>> fib.call_count # 49 calls, that's fine
49

2 Answers 2

2

f.call_count is 1 because f calls fib, and then fib recursively calls itself to calculate the result. In this whole procedure, f is only called once.

But when you do fib = decor(fib), you're overwriting the fib function in the global scope, so from that moment on fib will recursively call the decorated fib.

Sign up to request clarification or add additional context in comments.

Comments

0

You are overwriting fib and then f calls fib instead of itself:

def fib(n):
    if n==1 or n==0:
        return n
    else:
        print("fib is: " + repr(fib))
        return fib(n-1) + fib(n-2)

def decor(f):
    def counted(*args):
        counted.call_count += 1
        return f(*args)
    counted.call_count = 0
    return counted



f = decor(fib)
fib = decor(fib)

print("=== f ===")
print(f(6))
print(f.call_count)

print("=== fib ===")
print(fib(6))
print(fib.call_count)

When I run this the output is:

=== f ===
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
8
1
=== fib ===
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
fib is: <function counted at 0x7fa844639758>
8
49

Note that fib is function at 0x7fa844639758 all the time.

1 Comment

is it means that fib is the same at the global scope ? i assume binding fib again to decor(fib) returns a new function which is not related to the 'old' fib, exactly as f does.... is it something specific to python ? make me think i miss some basic language understanding. seems obvious that rebind fib should return same as f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.