Algorithm
# 1. Define a function to get the class name
def get_class_name(instance):
# 2. Use the `type` function to get the class type of the instance
class_name = type(instance).__name__
# 3. Return the class name
return class_name
# 4. Example usage
# - Create an instance of a class
# - Call the function to get the class name
example_instance = SomeClass()
result = get_class_name(example_instance)
# 5. Print or use the result as needed
print("Class Name:", result)
Code Examples
#1 Code Example- Python Programing Using __class__.__name__
Code -
Python Programming
class Vehicle:
def name(self, name):
return name
v = Vehicle()
print(v.__class__.__name__)
Copy The Code &
Try With Live Editor
Output
#2 Code Example- Python Programing Using type() and __name__ attribute
Code -
Python Programming
class Vehicle:
def name(self, name):
return name
v = Vehicle()
print(type(v).__name__)
Copy The Code &
Try With Live Editor
Output
Demonstration
Python Programing Example to Get the Class Name of an Instance-DevsEnv