Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

2
  • A key difference is that statements and expressions in the body of a function are evaluated when the function is called; the statements and expressions in the body of a class statement are executed as part of the class statement (i.e., earlier). In the function, some_condition may or may not be true, so x needs to have the same scope each time. The class statement is executed once, so by the time print x is reached, we know if x is local or global by the time we reach it. Commented Nov 27, 2013 at 19:08
  • My point is that in functions we know if x is a local or global by the time the bytecode compiler runs, before evaluating anything at all. More complicated examples can be drawn with a for loop in the class body where the same use of the same variable x is not always resolved to the same scope. Commented Nov 27, 2013 at 19:13