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 String encode()
Python Program to Solve Quadratic Equation
Python Program to Display the multiplication Table
Python Set issuperset()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Create a Long Multiline String
Python if...else Statement
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Transpose a Matrix
Python Program to Get the Full Path of the Current Working Directory
Python Set difference_update()
Python String istitle()
Python Tuple index()
Python timestamp to datetime and vice-versa
Python Type Conversion and Type Casting
Python Set union()
Python Inheritance
Python bytes()
Python Program to Trim Whitespace From a String
Python globals()
Python complex()
Python String maketrans()
Python Program to Capitalize the First Character of a String
Python Program to Check if a Key is Already Present in a Dictionary
Python oct()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String isnumeric()
Python input()
Python help()
Python String swapcase()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Iterators