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 delattr()
Python Dictionary keys()
Python Program to Get the File Name From the File Path
Python dir()
Python Program to Print Colored Text to the Terminal
Python str()
Python oct()
Debug a JavaMail Program
Python Program to Get the Last Element of the List
Python Function Arguments
Python Program to Iterate Through Two Lists in Parallel
Python String maketrans()
Python String encode()
Python Machine Learning - Sebastian Raschka
Python List clear()
Python String rstrip()
Python Program to Find Armstrong Number in an Interval
Python Global Keyword
Python List extend()
Python Program to Count the Number of Each Vowel
Python break and continue
Python bytearray()
Python Objects and Classes
Python Global, Local and Nonlocal variables
Python Program to Add Two Numbers
Python locals()
Python Dictionary popitem()
Python String lower()
Python Set difference_update()
Python Input, Output and Import
Python Anonymous / Lambda Function
Python repr()