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
v
of 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 __import__()
Python timestamp to datetime and vice-versa
Python iter()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Set issubset()
Python Dictionary keys()
Python List copy()
Python dir()
Python Program to Randomly Select an Element From the List
Python Program to Swap Two Variables
Python List extend()
Python Program to Differentiate Between type() and isinstance()
Python round()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Check if a Key is Already Present in a Dictionary
Python String rsplit()
Python Shallow Copy and Deep Copy
Python Dictionary items()
Python hex()
Python Program to Display Calendar
Python divmod()
Python Program to Merge Two Dictionaries
Python Program to Check Prime Number
Python String rpartition()
Python String partition()
Python Deep Learning Cookbook - Indra den Bakker
Python vars()
Python dict()
Python datetime
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Access Index of a List Using for Loop
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli