I am not very familiar with Python. So I have some problems writing code.
It is very normal to use function name function block in function block , for example:
def f(n): if n == 1: return n else: return n * f(n-1)
But when I try to use class name class block , everything goes wrong:
class Foo(object): a = Foo NameError: name 'Foo' is not defined
Although the code below is fine:
class Foo(object): def __init__(self): a = Foo
Then I debug these two codes using the print globals() operator. I found that the global dict variable in class block does not contain class Foo while the global dict variable in __init__ function block contains it.
Thus, it seems that class name binding occurs after class block execution and before function block execution.
But I do not like guesswork in the fundamental field of coding. Can anyone suggest a better explanation or official material on this?
source share