Why is n .__ name__ an attribute error when type (n) .__ name__ works?

n = 20
print n.__name__

I get an error because it ndoes not have an attribute __name__:

AttributeError: 'int' object has no attribute '__name__'

But it nis an instance of the class int, and int.__name__gives a result, so why it n.__name__gives an error. I expected that since it nis an instance of a class int, it should have access to all the attributes of this class.

+4
source share
1 answer

__name__is not an attribute of a class int(or any of its base classes):

>>> int.__dict__['__name__']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '__name__'
>>> int.__mro__
(<class 'int'>, <class 'object'>)
>>> object.__dict__['__name__']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '__name__'

, type ( descriptor, int int):

>>> type(int)
<type 'type'>
>>> type.__dict__['__name__']
<attribute '__name__' of 'type' objects>
>>> type.__dict__['__name__'].__get__(int)
'int'

, , .

.

+7

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


All Articles