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 tuple()
Python Program to Create a Countdown Timer
Python Program to Find Sum of Natural Numbers Using Recursion
Python Get Current time
Python Program to Check Armstrong Number
Python Program to Convert Celsius To Fahrenheit
Python Objects and Classes
Python Generators
Python oct()
Python ascii()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Variables, Constants and Literals
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Trim Whitespace From a String
Python List
Python String replace()
Python Tuple
Injecting Prototype Beans into a Singleton Instance in Spring
Python dict()
Python datetime
Python Set difference_update()
Python Program to Find the Square Root
Python Tuple count()
Python strftime()
How to Get Started With Python?
Python Program to Generate a Random Number
Python String format_map()
Python String title()
Python String zfill()
Python Program to Check If a String Is a Number (Float)
Python compile()
Python Set union()