Getting the class name of an instance?

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

    Good question.

    Here's a simple example based on GHZ's which might help someone:

    >>> class person(object):
            def init(self,name):
                self.name=name
            def info(self)
                print "My name is {0}, I am a {1}".format(self.name,self.__class__.__name__)
    >>> bob = person(name='Robert')
    >>> bob.info()
    My name is Robert, I am a person
    

提交回复
热议问题