Honza Šeda
Honza Šeda

Reputation: 45

Get the full name of a nested class

Given a nested class B:

class A:
    class B:
        def __init__(self):
            pass

ab = A.B()

How can I get the full name of the class for ab? I'd expect a result like A.B.

Upvotes: 2

Views: 1491

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160407

You could get the fully qualified name, __qualname__, of its __class__:

>>> ab.__class__.__qualname__
'A.B'

Preferably by using type (which calls __class__ on the instance):

>>> type(ab).__qualname__

Upvotes: 10

Related Questions