“local variable referenced before assignment” ― only functions?

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

Take the following code:

import something  def Foo():     something = something.SomeClass()     return something 

…this is apparently not valid code:

UnboundLocalError: local variable 'something' referenced before assignment 

…as the local variable something is created, but not assigned, before the RHS of the = is evaluated. (See, for example, this related answer's comment.) This seems a bit odd to me, but sure, I'll go with it. Now, why is the following valid code?

class Foo(object):     something = something.SomeClass() 

My understanding was that the inside of a class definition was essentially a scope:

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.

So, then, why does that code act differently than that of a function?

回答1:

From the python class documentation:

Class definitions place yet another namespace in the local scope.

So within a function (or a scope) the assignment creates a local unbound variable that is accessed before it is bound, whereas in a class definition it creates an entry in the "namespace" dictionary of that class on assignment, allowing the resolution of something to the outer namespace (the module namespace).



回答2:

Consider the following example which may help to clarify this:

import datetime  class Foo(object):     datetime = datetime.datetime  >>> datetime  >>> Foo.datetime 

Note that the line datetime = datetime.datetime is actually assigning to the name Foo.datetime, which is not ambiguous with the global datetime (like it would be if the same code were in the function).

In summary, because class definitions create a new namespace as well as a new scope, you are allowed to directly access a name in an enclosing scope and assign to the same name in the local scope.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!