Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda