Python class variable name vs __name__

I am trying to understand the relationship between the variable that the Python class object is assigned to and the __name__ attribute for this class object. For instance:

 In [1]: class Foo(object): ...: pass ...: In [2]: Foo.__name__ = 'Bar' In [3]: Foo.__name__ Out[3]: 'Bar' In [4]: Foo Out[4]: __main__.Bar In [5]: Bar --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-962d3beb4fd6> in <module>() ----> 1 Bar NameError: name 'Bar' is not defined 

So, it looks like I changed the __name__ attribute of this class, but I can't reference it with that name. I know this is a bit general, but can anyone explain the relationship between Foo and Foo.__name__ ?

+6
source share
4 answers

It's simple. There is no connection.

When you create a class, a local variable is created with the name that you used, pointing to the class so you can use it.

The class also receives the __name__ attribute, which contains the name of this variable, because it is convenient in some cases, for example, etching.

You can set the local variable to something else or change the __name__ variable, but then things like etching will not work, so don't do this.

+9
source

__name__ is just self-identification to find out what type of instance really is.

Another way is to access it. This may change if you reappoint it.

Both are assigned during class definition.

It works the same as with functions: if you def them, they are assigned to that name and get the corresponding __name__ attribute.

OTOH, if you have a lambda function, it gets the __name__ <lambda> attribute because it does not know the name to which it is assigned.

+3
source

Short version

class Foo(object): pass creates a class and assigns it to the local name Foo .

Foo.__name__ = 'Bar' assigns a new value to the __name__ attribute. Scope does not change.

Long version

The class operator creates the class and assigns a name specified in the local scope. When creating a class, Python tells the class the name with which it was created, assigning it to the __name__ class.

Assigning a class attribute does not enter a name in the local scope. Therefore, any changes to the attributes (for example, __name__ ) do not affect the application area.

+1
source

You need to keep in mind that in python a class is just an object, like any other. It would be impractical for an object to contain an attribute that was associated with a variable that refers to the object, because there can be any number of variable names related to it. Each time you write a task ( Bar = Foo ) or pass an object to a function, you have a new link. Naturally, all objects must be independent of how they are referenced.

__name__ is just a piece of information tied to a class object, which, as it turns out, matches the name of the variable to which it was originally assigned.

0
source

Source: https://habr.com/ru/post/952074/


All Articles