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 Set isdisjoint()
Python Dictionary get()
Python hasattr()
Python String format()
Python Program to Make a Flattened List from Nested List
Python Set discard()
Python String lower()
Python Inheritance
Python Program to Compute the Power of a Number
Python String rpartition()
Python File I/O Operation
Python Program to Print the Fibonacci sequence
Python Set update()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Iterate Through Two Lists in Parallel
Python Program to Check the File Size
Python strptime()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python slice()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Find Armstrong Number in an Interval
Python bytes()
Python Program to Differentiate Between type() and isinstance()
Deep Learning with Python - Francois Cholletf
Python String strip()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python format()
Python Program to Reverse a Number
Python Program to Trim Whitespace From a String
Python Program to Count the Number of Digits Present In a Number
Python Dictionary clear()