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
vof 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 List append()
Python delattr()
Python Program to Check Whether a String is Palindrome or Not
Python Data Types
Python __import__()
How to Get Started With Python?
Python String casefold()
Python Numbers, Type Conversion and Mathematics
Python String strip()
Python Closures
Python Namespace and Scope
Python Program to Find Factorial of Number Using Recursion
Python property()
Python Set issubset()
Python String title()
Python hex()
Python String swapcase()
Python dict()
Python Program to Catch Multiple Exceptions in One Line
Python callable()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python dir()
Python slice()
Python Program to Create a Long Multiline String
Python Program to Add Two Numbers
Python isinstance()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
APIs in Node.js vs Python - A Comparison
Python String format()
Python String rfind()
Python String encode()
Python Program to Check if a Number is Odd or Even