Why the difference in handling unbound locals in functions versus classes?
问题 When referencing global variables, one can see that functions and classes handle this differently. The first is fine and the second causes an error: x = 10 class Foo(): x = x + 1 a = foo() Vs: x = 10 def faa(): x = x + 1 faa() In the Python execution model, this is described as: A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global