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:]))
?