Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
3 views
by (16.4k points)

How would I discover a name of the class that made an instance of an object in Python if the function I am doing this from is the base class of which the class of the case has been inferred? 

Was thinking possibly the inspect module may have assisted me with excursion, yet it doesn't appear to give me what I need. Also, short of parsing the __class__ part, I don't know how to get at this data.

1 Answer

0 votes
by (26.4k points)

Have you attempted the __name__ attribute of the class? ie type(x).__name__ will give you the name of the class, which I believe is the thing that you need. 

>>> import itertools

>>> x = itertools.count(0)

>>> type(x).__name__

'count'

In case you're actually utilizing Python 2, note that the above strategy works with new-style classes just (in Python 3+ all classes are "new-style" classes). Your program may utilize some old-style classes. The accompanying works for both:

x.__class__.__name__

Wanna become a python expert? Come and join the python certification course and get certified.

Related questions

+3 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...