Table of Contents
In this example, you will learn to get the class name of an instance.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Using __class__.__name__
class Vehicle:
def name(self, name):
return name
v = Vehicle()
print(v.__class__.__name__)
Output
Vehicle
__class__ is the attribute of the class to which it is associated and __name__ is a special variable in Python. Its functionality depends on where it is used.
- Create an object
vof classVehicle(). - Print the name of the class using
__class__.__name__.
2. Example 1: Using type() and __name__ attribute
class Vehicle:
def name(self, name):
return name
v = Vehicle()
print(type(v).__name__)
Output
Vehicle
Using attribute __name__ with type(), you can get the class name of an instance/object as shown in the example above. type() gives the class of object v and __name__ gives the class name.
Related posts:
Python Program to Shuffle Deck of Cards
Node.js vs Python for Backend Development
Python setattr()
Python Program to Illustrate Different Set Operations
Python Program to Check Prime Number
Python Program to Differentiate Between type() and isinstance()
Python Program to Find All File with .txt Extension Present Inside a Directory
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Sort a Dictionary by Value
Python Program to Find Hash of File
Python dict()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python complex()
Python Program to Display Fibonacci Sequence Using Recursion
Python help()
Python Program to Find the Sum of Natural Numbers
Python repr()
Python staticmethod()
Python String islower()
Python String format_map()
Python @property decorator
Injecting Prototype Beans into a Singleton Instance in Spring
Python while Loop
Python Generators
Python open()
Python Program to Find the Factorial of a Number
Python zip()
Python Statement, Indentation and Comments
Python Program to Find the Factors of a Number
Python Custom Exceptions
Python Set isdisjoint()