Python

Get a Class Name in Python

Fetching an object’s class name is an easy task in Python, especially if you know exactly what to do. There are certain attributes that the user can use to find the class name of either default objects (variables) or a custom/user-build class. The classes are used to create modularly-reusable code with some added features, and having multiple classes can confuse the user. Therefore, finding the class name of an object/variable is quite crucial.

The post will showcase the following content to display an object’s class name:

Method 1: Using the type() Method

The most fundamental method for figuring out an object’s class name is the type() method. The type method takes in a variable/object and returns its class. To demonstrate the working of the type() method. Simply take the following code:

stringVar = "Google Is Amazing"
print(type(stringVar))

When this code is executed, it will show the following display on the terminal:

The output shows that the class name of the variable stringVar is “str.”

Method 2: Using the __class__ Attribute

Every object that is created in the Python program language has a few attributes that are attached to it using its prototype. These methods do not need to be attached to the object by the user. Instead, these are attached by created by default at the time of the creation of the object. The __class__ is one such attribute that stores the information about the object’s class, and you can access it using the dot operator.

To demonstrate the use of the __class__ attribute, take the following code:

numVar = 5965
print(numVar.__class__)

When this code is executed, it will produce the following outcome:

The same approach can be used for user build classes as well, such as:

class person:
    def __init__(self, name):
        self.animal = name

per1 = person("John Doe")

print (per1.__class__)

When this code is executed, it will produce the following result:

As you can see, the class is printed as “person.” However, if you want to fetch only the name of the class without any structuring or wrapping, as shown in these examples, then you can use the next method.

Method 3: Using the __class__.__name__ to Get Only the Class Name

Simply use the __class__.__name__ attribute to get only the name of the class. To demonstrate this with a user build class, take the following code snippet:

class person:
    def __init__(self, name):
        self.animal = name

per1 = person("John Doe")

print (per1.__class__.__name__)

This code snippet will produce the following outcome when executed by the user:

As you can see, this time around only the class name was printed on the terminal. You can also use the same approach for the predefined classes, and for that, take the following code:

stringVar = "LinuxHint Python Tutorials"
print(stringVar.__class__.__name__)

The output of this code is as:

The class of the stringVar variable is named “str.”

Conclusion

To get the class name of an object/variable the user can use the type() method. For the type() method, simply pass the variable inside the type() method as an argument. Other than that, the user can use the __class__ attribute with the help of a dot operator. Both of these methods return a specific structural output that contains the class name. In case, the user wants to get only the class name, then the user has to use the __name__ attribute along with the __class__ attribute.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.