Getting the class name of an instance?

前端 未结 10 2147
情书的邮戳
情书的邮戳 2020-11-22 13:38

How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance h

10条回答
  •  囚心锁ツ
    2020-11-22 14:14

    You can simply use __qualname__ which stands for qualified name of a function or class

    Example:

    >>> class C:
    ...     class D:
    ...         def meth(self):
    ...             pass
    ...
    >>> C.__qualname__
    'C'
    >>> C.D.__qualname__
    'C.D'
    >>> C.D.meth.__qualname__
    'C.D.meth'
    

    documentation link qualname

提交回复
热议问题