I’m surprised that the namespace of function parameters in a method is the class and not the global scope.
def a(x):
print("Global A from {}".format(x))
class Test:
def a(self, f=a):
print("A")
f("a") # this will call the global method a()
def b(self, f=a):
print("B")
f("b") # this will call the class method a()
t=Test()
t.b()
How to explain that? And how would I access the global a() from the parameters of b?
Namespace lookups always check the local scope first. In a method definition, that is the class.
At the time of the definition of
Test.a, there is no local nameda, only the globala. By the timeTest.bis defined,Test.ahas already been defined, so the local nameaexists, and the global scope is not checked.If you want to point
finTest.bto the globala, use:which prints
as expected.