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 Generators
Python map()
Python Machine Learning - Sebastian Raschka
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Set remove()
Python Program to Print Hello world!
Python any()
Python Program to Solve Quadratic Equation
Python Set copy()
Python Set difference()
Python Dictionary keys()
Python Dictionary fromkeys()
Python Data Structures and Algorithms - Benjamin Baka
Python strptime()
Python Variables, Constants and Literals
Python String endswith()
Python Program to Make a Simple Calculator
Python eval()
Python Program to Print Output Without a Newline
Python String maketrans()
Python Program to Add Two Numbers
Python float()
Python String isspace()
Python Program to Randomly Select an Element From the List
Python Global, Local and Nonlocal variables
Python Dictionary setdefault()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python String format()
Python compile()
Python sorted()
Python Directory and Files Management
Python Set symmetric_difference_update()