According to PEP3155 __qualname__
was introduced on class
and function
objects.
Now, we need to remember that in Python everything is an object. Functions are objects. Classes are objects! And here we don't mean "instances of classes" are objects, but the classes themselves are objects.
Example:
>>> class C:
... def __init__(self, a): self.a = a
...
>>> c = C(2)
>>> type(c)
<class '__main__.C'>
>>> hasattr(c, '__qualname__')
False
>>> hasattr(C, '__qualname__')
True
>>> C.__qualname__
'C'
So the class C
has __qualname__
but instances of this class do not have it. You can retrieve it with an indirection, however: c.__class__.__qualname__
(small c
)
Similarly for functions:
>>> def f(b): print(b)
...
>>> hasattr(f, '__qualname__')
True
>>> f.__qualname__
'f'
If you nest classes and functions, their __qualname__
will indicate this definition hierarchy. See What is qualname