|
|
|
The reason is quite simple Michael.
>>> class A: pass
...
>>> A.__name__
'A'
>>> a = A()
>>> c = a
>>>
Which name should have this "instance" ? c and a are alias (name bindings) of the same object. How could python possibly determine which is the right one? A variable name is just a binding, an alias of a "memory cell". I think there's no point in knowing the actual name of the alias. You can cook something up with dictionaries if you really have to.
HTH
Lawrence Oluyede |
Homepage |
07/02/18 - 4:12 pm | #
|
|
That's a fair enough 'why', but doesn't explain the 'how'. 
If the class has an attribtue then the instance ought to inherit it.
The answer is that __name__ *doesn't* exist on the class, but is looked up on the metaclass - which is still a bit weird...
Thanks
Michael
Fuzzyman |
Homepage |
07/02/18 - 4:24 pm | #
|
|
Why is it weird? A class is an instance of the metaclass. Attribute lookup on a class naturally pays attention to the metaclass.
__name__ is a data descriptor defined by the 'type' metaclass:
>>> type.__dict__['__name__'].__get__(object)
'object'
>>> type.__dict__['__name__'].__get__(type)
'type'
>>> type.__dict__['__name__'].__get__(dict)
'dict'
>>> type.__dict__['__name__'].__get__(list)
'list'
I do not know where it gets the class name from its argument. Probably from a structure member that is only accessible at the C level. If you really want to know how this works, you probably need to read the Python source code.
Marius Gedminas |
Homepage |
07/02/18 - 4:34 pm | #
|
|
|
Commenting by HaloScan
|