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:
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Iterate Over Dictionaries Using for Loop
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Dictionary get()
Python Dictionary popitem()
Python Program to Count the Number of Each Vowel
Python Set union()
Python eval()
Python Program to Find the Square Root
Python String lower()
Python ord()
Python Program to Add Two Numbers
Python Dictionary values()
Python File I/O Operation
Python Package
Python String capitalize()
Python Program to Convert Bytes to a String
Python hash()
Python max()
Python Set pop()
Python String title()
Python Program to Get File Creation and Modification Date
Python frozenset()
Python Numbers, Type Conversion and Mathematics
Python bytes()
Python set()
Python Program to Safely Create a Nested Directory
Python String strip()
Python Modules
Python Generators
Python Program to Find Numbers Divisible by Another Number