Python Program to Get the Class Name of an Instance – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Get the Class Name of an Instance.

Example 1: Using __class__.__name__

class Project:
    def name(self, name):
        return name

p = Project()
print(p.__class__.__name__)

Output

Project
  • We can use object.__class__.__name__ to get the class name of the object.

Example 2: Using type() and __name__ attribute

class Project:
    def name(self, name):
        return name

p = Project()
print(type(p).__name__)

Output

Project
  • We can also use type(object).__name__ to get the class name of the object.

Check out our other python programming examples

Leave a Comment

Your email address will not be published. Required fields are marked *