>> type('abc') >>> type(1) <...">

Get a "type" return as a human-readable string

How to get the variable part of type as a string?

t

 >>> type('abc') <type 'str'> >>> type(1) <type 'int'> >>> type(_) <type 'type'> 

In each case, here I want what's inside single quotes: str, int, type as a string.

I tried using regex against repr(type(1)) and it works, but it doesn't seem reliable or Pythonic. Is there a better way?

+6
source share
4 answers

You can get the name type(1).__name__

+6
source

use the __name__ attribute of the type object:

 In [13]: type('abc').__name__ Out[13]: 'str' In [14]: type(1).__name__ Out[14]: 'int' 
+3
source

What about ... .__class__.__name__ ?

 >>> 'abc'.__class__.__name__ 'str' >>> a = 123 >>> a.__class__.__name__ 'int' 
+3
source

Use the __name__ attribute:

 >>> type('abc').__name__ 'str' 
+1
source

All Articles