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 Functions
Python Program to Create a Countdown Timer
Python Data Types
Python Program to Find Factorial of Number Using Recursion
Python Iterators
Python Custom Exceptions
Python int()
Python Tuple index()
Python Program to Copy a File
Python isinstance()
Python List index()
Python Program to Represent enum
Python Program to Find HCF or GCD
Python Program to Count the Number of Occurrence of a Character in String
Python Set issuperset()
Python map()
Python String lstrip()
Python Program to Remove Duplicate Element From a List
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Program to Access Index of a List Using for Loop
Python String isdecimal()
Python Program Read a File Line by Line Into a List
Python Program to Safely Create a Nested Directory
Python Inheritance
Python Program to Parse a String to a Float or Int
Python setattr()
Python Anonymous / Lambda Function
Python Program to Compute all the Permutation of the String
Python hex()
Python reversed()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python String upper()