Check if string is type
Python normally looks up names using LEGB. Since you have no non-locals, you can ignore E
. You know that you don't have a local name, so L
is gone too. So the equivalent lookup would indeed be a call to globals
and a search of builtins
.
You don't need a dictionary if all you care about are the keys. That way, you explicitly pass in simple strings and don't need to play games with extra characters:
import builtins
from inspect import isclass
def convert(target, *names):
for name in names:
obj = globals().get(name, getattr(builtins, name, None))
if isclass(obj):
return obj(target)
return converting