import scratch
print(id(globals()), ":main")
## these are the same
F = scratch.Fibonacci(100)
print(id(F.__init__.__globals__))
print(id(scratch.fibo.__globals__))
print(id(eval("globals()", scratch.__dict__)))
print(id(eval("globals()", F.__dict__)), "?")
Now my question is, “where did the last value come from? or where is the last globals?”.
I expected that the last value should be the same as the previous three.
The second argument of eval is the globals namespace where the code is evaluated.
eval("globals()", F.__dict__)
My understanding this time is that the above expression does not mean F’s globals, but globals() evaluated in F.__dict__.
The globals() builtin function returns the namespace where the current code is evaluated, that is, F.__dict__.
globals ()
Return the dictionary implementing the current module namespace. For code within functions, this is set when the function is defined and remains the same regardless of where the function is called. Built-in Functions — Python 3.10.13 documentation