Getting the class name of an instance?

前端 未结 10 2148
情书的邮戳
情书的邮戳 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'
    >>>
    
    0 讨论(0)
  • 2020-11-22 14:02

    Apart from grabbing the special __name__ attribute, you might find yourself in need of the qualified name for a given class/function. This is done by grabbing the types __qualname__.

    In most cases, these will be exactly the same, but, when dealing with nested classes/methods these differ in the output you get. For example:

    class Spam:
        def meth(self):
            pass
        class Bar:
            pass
    
    >>> s = Spam()
    >>> type(s).__name__ 
    'Spam'
    >>> type(s).__qualname__
    'Spam'
    >>> type(s).Bar.__name__       # type not needed here
    'Bar'
    >>> type(s).Bar.__qualname__   # type not needed here 
    'Spam.Bar'
    >>> type(s).meth.__name__
    'meth'
    >>> type(s).meth.__qualname__
    'Spam.meth'
    

    Since introspection is what you're after, this is always you might want to consider.

    0 讨论(0)
  • 2020-11-22 14:04
    class A:
      pass
    
    a = A()
    str(a.__class__)
    

    The sample code above (when input in the interactive interpreter) will produce '__main__.A' as opposed to 'A' which is produced if the __name__ attribute is invoked. By simply passing the result of A.__class__ to the str constructor the parsing is handled for you. However, you could also use the following code if you want something more explicit.

    "{0}.{1}".format(a.__class__.__module__,a.__class__.__name__)
    

    This behavior can be preferable if you have classes with the same name defined in separate modules.

    The sample code provided above was tested in Python 2.7.5.

    0 讨论(0)
  • 2020-11-22 14:07

    Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of the class, which I think is what you want.

    >>> import itertools
    >>> x = itertools.count(0)
    >>> type(x).__name__
    'count'
    

    If you're still using Python 2, note that the above method works with new-style classes only (in Python 3+ all classes are "new-style" classes). Your code might use some old-style classes. The following works for both:

    x.__class__.__name__
    
    0 讨论(0)
  • 2020-11-22 14:07

    Do you want the name of the class as a string?

    instance.__class__.__name__
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题