Python: get type object from its name?

Viewed 1172

Is there a way to get a type object, given the name of that type? So, for example:

   get_type('str') -> str

I am building a dict mapping various cases to a class which should be instantiated to handle them. It seems overkill to import everything and risk circular imports. So I thought I could specify them with strings, and look up the type at point of use. But how do I look up the type?

It seems unlikely that this hasn't been asked before, but I've searched and not found it.

1 Answers

You can get the builtin types by checking attribute on the builtins module using getattr:

In [665]: import builtins 
In [666]: def get_type(type_name): 
     ...:     try: 
     ...:         return getattr(builtins, type_name) 
     ...:     except AttributeError: 
     ...:         return None 
     ...:                                                                                                                                                                                                   

In [667]: get_type('str')                                                                                                                                                                                   
Out[667]: str

In [668]: get_type('list')                                                                                                                                                                                  
Out[668]: list

In [669]: get_type('dict')                                                                                                                                                                                  
Out[669]: dict

FWIW, you can replace the AttributeError catching with passing the third parameter to getattr which acts as a default when attribute is missing (thanks @Error - Syntactical Remorse for the reminder):

def get_type(type_name):
    return getattr(builtins, type_name, None)

For handling custom types, you can peek into the globals dict:

In [670]: def get_type(type_name): 
     ...:     try: 
     ...:         return getattr(builtins, type_name) 
     ...:     except AttributeError: 
     ...:         try: 
     ...:             obj = globals()[type_name] 
     ...:         except KeyError: 
     ...:             return None 
     ...:         return repr(obj) if isinstance(obj, type) else None 
     ...:                                                                                                                                                                                                          

In [671]: class B: 
     ...:     pass 
     ...:                                                                                                                                                                                                   

In [672]: get_type('B')                                                                                                                                                                                     
Out[672]: "<class '__main__.B'>"

In [673]: get_type('C') is None
Out[673]: True
Related