How to Get Class Name in Python?

This blog post will explain how to get class name in Python. There are a few different methods that can help you find the class name of an object.

Before diving in, let’s first remember what class is in Python.
In Object-Oriented Programming (OOP), a class is a blueprint defining the methods and variables of a particular kind of object. Similarly, in Python, a class defines how the particular object should look like, or more precisely, what variables and methods it should contain.

In some cases, you’d want to check the name of the class of a particular object, so let’s find out how to do it.

How to get class name of an object in Python?
The most convenient way to get Python class name is either by using the type() or __class__ method. To get the class name as a string, you will also need to use the __name__ variable regardless of which of the two methods you use.

If you are using the type() method, the syntax is:
type(x).__name__

If you are using the __class__ method, the syntax is:
x.__class__.__name__

x is the object for which you are getting the class name.

In the following sections, I’ll explain these methods in more detail.

Use type() to get class name in Python

One way of finding the Python class name of an object is by using the built-in type() method. The type() method returns the data type of the object passed to it as an argument. 

Still, to get the class name as string, you need to access the special built-in __name__ variable. This variable evaluates to the name of the current module. By chaining the type() and __name__, you get exactly what you want – the Python class name for the current object.

Let’s see it in action. The first example will show you how to get the class name of some well-known Python literals and data structures.

>>> a = 45
>>> type(a).__name__
'int'

>>> b = 'example'
>>> type(b).__name__
'str'

>>> c = True
>>> type(c).__name__
'bool'

>>> d = [1, 2, 3]
>>> type(d).__name__
'list'

>>> e = {1: 'abc'}
>>> type(e).__name__
'dict'

Also, check out this example for a user-defined class. Here we define the simple Book class that accepts the name in the constructor.

class Book:
  def __init__(self, name):
    self.name = name

book = Book("The Hitchhiker's Guide to the Galaxy")
print(type(book).__name__)  # Book

As you can see, the type() method in combination with the __name__ variable returned correct class name.

Use __class__ to get class name in Python

Another convenient way to get class name in Python is by using the built-in __class__ attribute. __class__ is an attribute on the object that refers to the class from which the object was created. Since it returns the object, you have to use it again in combination with the __name__ variable to get class name as string.

We’ll use the same examples as in the previous section to show how to use the __class__ attribute.

>>> a = 45
>>> a.__class__.__name__
'int'

>>> b = 'example'
>>> b.__class__.__name__
'str'

>>> c = True
>>> c.__class__.__name__
'bool'

>>> d = [1, 2, 3]
>>> d.__class__.__name__
'list'

>>> e = {1: 'abc'}
>>> e.__class__.__name__
'dict'

Now let’s see it in action with our custom Book class.

class Book:
  def __init__(self, name):
    self.name = name

book = Book("The Hitchhiker's Guide to the Galaxy")
print(book.__class__.__name__)  # Book

Same as before, we got the correct class name.

NOTE - If you are still using Python 2, you should stick to using this method of getting class name since it supports old-style and new-style Python classes. Unfortunately, the type() method works only with new-style classes.

Bonus – define a custom whoami method

To make your life a bit easier when working with multiple custom classes, you could implement a custom method to tell you what class or type is particular object. That way, you won’t need to remember how to find the class name every time. 

In short, you should define a parent class that will implement the custom, let’s call it – whoami, method. By inheriting the parent class, each child class will have a whoami method available.

In our example, we’ll call the parent class A, and the child class B. Of course, in your case you can have as many children classes as you want in your case.

class A:
  def whoami(self):
    return type(self).__name__

class B(A):
  pass

b = B()
print(b.whoami())  # B

I hope I helped you!