w3resource

Python: Get the class name of an instance in Python


12. Get the Class Name of an Instance

Write a Python class to get the class name of an instance in Python.

Sample Solution:

Python Code:

import itertools
x = itertools.cycle('ABCD')
print(type(x).__name__)

Sample Output:

cycle

Flowchart:

Flowchart: Get the class name of an instance in Python

For more Practice: Solve these Related Problems:

  • Write a Python program to create an instance of any class and then print its class name using the __class__.__name__ attribute.
  • Write a Python program to implement a function that takes an object and returns its class name without using type().
  • Write a Python program to dynamically determine and print the class names of elements in a list of heterogeneous objects.
  • Write a Python program to compare the class names of two objects and print a message if they belong to the same class.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle.
Next: Data Structures and Algorithms Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.