1

I was doing some introspection on pandas objects when I encountered an error for pandas.core.indexing._iLocIndexer.

The docs say:

definition.__qualname__

The qualified name of the class, function, method, descriptor, or generator instance.

I'm wondering if all objects are expected to have __qualname__ (assuming Python 3.3+) and if not, what objects are those.

kentwait
  • 1,969
  • 2
  • 21
  • 42
  • No, the docs are explicitly telling *which* types of objects have that attribute: "The qualified name of the class, function, method, descriptor, or generator instance." – juanpa.arrivillaga Dec 22 '20 at 09:37

1 Answers1

1

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

Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65