In Python, given the string\'int\'
, how can I get the type int
? Using getattr(current_module, \'int\')
doesn\'t work.
int
isn't part of the namespace of your current module; it's part of the __builtins__
namespace. So you would run getattr
on __builtins__
.
To verify that it's a type you can just check whether it's an instance of type
, since all types are derived from it.
>>> getattr(__builtins__, 'int')
>>> foo = getattr(__builtins__, 'int')
>>> isinstance(foo, type)
True