osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Instance vs Class variable oddity


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