Getting the class name of an instance?

前端 未结 10 2247
情书的邮戳
情书的邮戳 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:13

    Alternatively you can use the classmethod decorator:

    class A:
        @classmethod
        def get_classname(cls):
            return cls.__name__
    
        def use_classname(self):
            return self.get_classname()
    

    Usage:

    >>> A.get_classname()
    'A'
    >>> a = A()
    >>> a.get_classname()
    'A'
    >>> a.use_classname()
    'A'
    

提交回复
热议问题