qualname

0.1.0 · active · verified Mon Apr 13

The `qualname` Python module emulates the `__qualname__` attribute for classes and methods in older Python versions (specifically pre-Python 3.3). Introduced in PEP 3155, `__qualname__` provides a 'qualified name' including the full dotted path to an object within its module, which is particularly useful for nested definitions. This library achieves this by performing source code inspection and abstract syntax tree (AST) parsing, which means the source file must be available at runtime. The current version is 0.1.0, and its release cadence is infrequent, as its primary utility lies in supporting older Python environments.

Warnings

Install

Imports

Quickstart

Demonstrates how to use the `qualname()` function to retrieve the qualified name for classes, methods, and nested functions. On Python 3.3+, native `__qualname__` is used, otherwise the library's emulation is applied.

import sys
from qualname import qualname

class Outer:
    def method_a(self):
        pass

    class Inner:
        def method_b(self):
            pass

def top_level_func():
    def nested_func():
        pass
    return nested_func

# For Python < 3.3, this library provides __qualname__ functionality
# On Python 3.3+, objects already have __qualname__

print(f"Python Version: {sys.version.split(' ')[0]}")
print(f"Outer.__qualname__: {qualname(Outer)}")
print(f"Outer.method_a.__qualname__: {qualname(Outer.method_a)}")
print(f"Outer.Inner.__qualname__: {qualname(Outer.Inner)}")
print(f"Outer.Inner.method_b.__qualname__: {qualname(Outer.Inner.method_b)}")

nested_f = top_level_func()
print(f"top_level_func.__qualname__: {qualname(top_level_func)}")
print(f"nested_f.__qualname__: {qualname(nested_f)}")

view raw JSON →