Getting the class name of an instance?

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

    type() ?

    >>> class A(object):
    ...    def whoami(self):
    ...       print type(self).__name__
    ...
    >>>
    >>> class B(A):
    ...    pass
    ...
    >>>
    >>>
    >>> o = B()
    >>> o.whoami()
    'B'
    >>>
    

提交回复
热议问题