How do I find out the name of the class used to create an instance of an object in Python?
I'm not sure if I should use the inspect
module or parse the __class__
attribute.
instanceofintrospectionpythonpython-datamodel
How do I find out the name of the class used to create an instance of an object in Python?
I'm not sure if I should use the inspect
module or parse the __class__
attribute.
Best Answer
Have you tried the
__name__
attribute of the class? ietype(x).__name__
will give you the name of the class, which I think is what you want.If you're still using Python 2, note that the above method works with new-style classes only (in Python 3+ all classes are "new-style" classes). Your code might use some old-style classes. The following works for both: