Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
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 namespace.

But why?

The only other hint I have come across is this bit:

The class’s suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. 4 A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary.

Which still offers no explanation why this should have the consequence that unbound locals are looked up in the global namespace.

Both links are from this answer which does not adress the why in more details though.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As explained in Python FAQ - Why am I getting an UnboundLocalError?

... because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in foo assigns a new value to x, the compiler recognizes it as a local variable.

Consequently when earlier attempting to access the uninitialized local variable an error results.

This explains why this:

x = 10
def foo():
    x = x+1  # raises UnboundLocalError
foo()

raises an Exception, but not this:

x = 10
def faa():
    y = x+1  # x stays in global scope, since it is not assigned in local
faa()

For the class foo() it's the same, since python allows assignments of attributes to objects to any given time. The class-code assigns x as new attribute of the object a.

x = 10
class foo():
    x = x+1  # scope of x: class
a = foo()  # x = 10; a.x = foo.x = 11

is the same as:

x = 10
class foo():
    def __init__(self):
        self.x = x+1  # scope of x: instance
a = foo()  # x = 10; a.x = 11

where obviously self.x is assigned instead of x, therefore also staying in global scope. (see also here)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!