Convert a python ‘type’ object to a string

print(type(some_object).__name__)

If that doesn’t suit you, use this:

print(some_instance.__class__.__name__)

Example:

class A:
    pass
print(type(A()))
# prints <type 'instance'>
print(A().__class__.__name__)
# prints A

Also, it seems there are differences with type() when using new-style classes vs old-style (that is, inheritance from object). For a new-style class, type(someObject).__name__ returns the name, and for old-style classes it returns instance.

Leave a Comment