Instance vs Class variable oddity

jfong at ms4.hinet.net jfong at ms4.hinet.net
Fri May 17 23:49:34 EDT 2019


Correct me if I am wrong, please.

I always think that the LEGB rule (e.g. the namespace to look up for) was applied at compile-time, only the binding was resolved "dynamically" at run-time. For example:

def foo():
    print(x)

foo() will cause a NameError. But after

x = 5

foo() will run correctly.

I also don't think the term "variable" and "attribute" can be used exchangeable. Variable apply to an unbound name, and attribute applies to bounded name. For example:

foo.x = 3 will create an attribute x of function foo. It's either not a local variable, or a global variable. It's more likely a class instance attribute, and it's resolved by applying MRO rule dynamically.

--Jach



More information about the Python-list mailing list