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 Data Types
Python Program to Concatenate Two Lists
Python Program to Convert Celsius To Fahrenheit
Python Program to Find ASCII Value of Character
Python getattr()
Python chr()
Python Program to Remove Duplicate Element From a List
Python List Comprehension
Python list()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python input()
Python Program to Solve Quadratic Equation
Python Deep Learning Cookbook - Indra den Bakker
Python String startswith()
Python Program to Count the Number of Digits Present In a Number
Python frozenset()
Python Program to Find the Factors of a Number
Python __import__()
Python Program to Check the File Size
Python Program to Swap Two Variables
Python Dictionary values()
Python range()
Python Program Read a File Line by Line Into a List
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Decorators
Debug a JavaMail Program
Python List sort()
Python iter()
Python Program to Represent enum
Python Dictionary fromkeys()
Python break and continue
Python String lower()