Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
recategorized by

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

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...