(I may have asked this before somewhere. If so, I don’t remember where, and don’t seem to be finding my original question. Still, if I have, then my apologies! ^^; )
In Python, is there a means of determining whether a given string (e.g. “int”) corresponds to the name of a built-in type?
By comparison, one can determine whether a string corresponds to a member of an instance by using “hasattr”, or by checking the member’s “__dict__” member, if I’m not much mistaken.
Is there some way of doing something similar for built-ins?
To explain, I’m trying to improve the security of my GameSaver module by removing "eval"s and "exec"s. Right now, an “eval” is used to reconstitute objects, running something like this:
# Given that "objType" contains a string type-name, like "int" finalVal = eval(objType + "(newVal)")
I want to replace this, but I don’t want to create an exhaustive list of:
if objType == 'int': finalVal = int(newVal) elif objType == "float": finalVal = float(newVal) # etc...
Not only does that become extensive, but it doesn’t seem terribly future-proof to me.
I’m hoping that there’s an easier way, something like or analogous to this:
if objType in builtIns: finalVal = builtIns[objType](newVal)
Or something similarly painless, and relatively future-proof.