I wish to parse keyword arguments to determine if they also refer to types, such as in the case below:
from inspect import isclass
def convert(converting, **kwargs):
for key, value in kwargs.items():
if value and isclass(eval(key[1:])):
return(eval(key[1:])(converting))
string = "Hello!"
print(convert(string, _list = True))
I am well aware that eval has security concerns for unknown strings, which is why I am looking for a safer method of determining the type from the keyword name.
Built-in types can be checked via import builtins; isclass(getattr(builtins, 'str')), as per a_guest's comment here, but I am still stumped on how to check other classes. Perhaps isclass(getattr(globals(), key[1:]))?
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, soLis gone too. So the equivalent lookup would indeed be a call toglobalsand a search ofbuiltins.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: