The __qualname__ attribute is useful to me because it contextualizes functions; however, it's difficult for me to use for my use case because:
__qualname__returns a string. For my usecase, I need references to the parent object(s).__qualname__sometimes returns thesuperclass instead of the referenced class. For example:class Parent(): def __init__(self): pass class Child(Parent): pass print(Child.__init__.__qualname__) # Prints: "Parent.__init__"The package I am developing needs to be robust, and the edge cases for
__qualname__are not documented as far as I can tell.
Outside of parsing the Python file with ast, can __qualname__ be reimplemented in Python3 with inspection? How does Python implement __qualname__? In reimplementing the core functionality, I think I'll be able to adapt it for my use case.
Prior Research:
- PEP 3155: https://www.python.org/dev/peps/pep-3155/
- Python 2
qualnameimplementation: https://github.com/wbolster/qualname/blob/master/qualname.py - Parsing
__qualname__to get references: Get defining class of unbound method object in Python 3 - Python 2
__qualname__stackoverflow: Reproduce effects of Python 3.3 __qualname__ in earlier Pythons
I was unable to find the qualname implementation in the Python source code.