Quick actions

cmd+k|ctrl+k

Navigation

Languages

Fibonacci F( )

Snippet info

Language

Python

Visibility

public

Author

2cool4gentoo

Created

2024-08-05T08:21:23.796524Z

Updated

2024-08-08T06:49:44.731431Z

import dis 
def fib(n):
    if n <= 2:
        return 1
    print(id(fib))
    return fib(n - 1) + fib(n - 2)

# Display the disassembled bytecode of the function    
dis.dis(fib)
print(id(fib(5)))


def fib1(n):
    if n < 2:
        return n
    current, next = 0,1
    while n:
        current, next = next, current + next
        n -=1
    return current
print(print(1))
print(fib1.__code__)  #the address of the function 
print(fib1.__code__.co_consts) # return triple, constants, first const none, why is that ? Why tuple ?
print(fib1.__code__.co_names) # all the local variable names, n is semi-local
print(fib1.__code__.co_code) # actual byte code
INFO