How to Get the Class Name of an Instance in Python

Created
Modified

Using type Method

The type(object) method returns the type of an object. For example,

#!/usr/bin/python3

class A:
  pass

a = A()
name = type(a).__name__
print(name)
A

Using Instance Classname

instance.__class__.__name__ represents the name of the class. For example,

#!/usr/bin/python3

class A:
  pass

a = A()
print(a.__class__.__name__)
A

Using Qualified Name

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

#!/usr/bin/python3

class C:
  class A:
    pass

print(C.A.__qualname__)
C.A

Related Tags