Getting the class name of an instance


How do I find out the name of the class used to create an instance of an object in Python?

I’m not sure if I should use the inspect module or parse the __class__ attribute.

Asked By: Dan



type() ?

>>> class A:
...     def whoami(self):
...         print(type(self).__name__)
>>> class B(A):
...     pass
>>> o = B()
>>> o.whoami()
Answered By: GHZ

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__

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:

Answered By: sykora

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

Answered By: mthurlin

Good question.

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

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

In Python 2,

type(instance).__name__ != instance.__class__.__name__
# if class A is defined like
class A():

type(instance) == instance.__class__
# if class A is defined like
class A(object):


>>> class aclass(object):
...   pass
>>> a = aclass()
>>> type(a)
<class '__main__.aclass'>
>>> a.__class__
<class '__main__.aclass'>
>>> type(a).__name__
>>> a.__class__.__name__

>>> class bclass():
...   pass
>>> b = bclass()
>>> type(b)
<type 'instance'>
>>> b.__class__
<class __main__.bclass at 0xb765047c>
>>> type(b).__name__
>>> b.__class__.__name__
Answered By: Prasath
class A:

a = A()

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.


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.

Answered By: Jonathan

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):
    class Bar:

>>> s = Spam()
>>> type(s).__name__ 
>>> type(s).__qualname__
>>> type(s).Bar.__name__       # type not needed here
>>> type(s).Bar.__qualname__   # type not needed here 
>>> type(s).meth.__name__
>>> type(s).meth.__qualname__

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

Alternatively you can use the classmethod decorator:

class A:
    def get_classname(cls):
        return cls.__name__

    def use_classname(self):
        return self.get_classname()


>>> A.get_classname()
>>> a = A()
>>> a.get_classname()
>>> a.use_classname()
Answered By: Yurii Rabeshko

To get instance classname:




both are the same

Answered By: v.babak

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


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

documentation link qualname

Answered By: Lalit Vavdara

You can first use type and then str to extract class name from it.

class foo:pass;



Answered By: Supergamer

If you’re looking to solve this for a list (or iterable collection) of objects, here’s how I would solve:

from operator import attrgetter

# Will use a few data types to show a point
my_list = [1, "2", 3.0, [4], object(), type, None]

# I specifically want to create a generator
my_class_names = list(map(attrgetter("__name__"), map(type, my_list))))

# Result:
['int', 'str', 'float', 'list', 'object', 'type', 'NoneType']

# Alternatively, use a lambda
my_class_names = list(map(lambda x: type(x).__name__, my_list))
Answered By: Yaakov Bressler