Reputation:
How do I get the variable part of type
as a string?
ie:
>>> type('abc')
<type 'str'>
>>> type(1)
<type 'int'>
>>> type(_)
<type 'type'>
In each case here, I want what is inside single quotes: str, int, type as a string.
I tried using a regex against repr(type(1))
and that works, but that does not seem robust or Pythonic. Is there a better way?
Upvotes: 9
Views: 1295
Reputation: 5013
How about ... .__class__.__name__
?
>>> 'abc'.__class__.__name__
'str'
>>> a = 123
>>> a.__class__.__name__
'int'
Upvotes: 3
Reputation: 251156
use the __name__
attribute of type
object:
In [13]: type('abc').__name__
Out[13]: 'str'
In [14]: type(1).__name__
Out[14]: 'int'
Upvotes: 3