ShadowRylander
ShadowRylander

Reputation: 405

Check if string is type

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

Upvotes: 2

Views: 114

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114440

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

Upvotes: 3

Related Questions