I have written a simple function that allows me to parse a string with a variable type (following the method described in this SO question Lexical cast from string to type). I have a module called ascii_tools that contains the following function:
def get_builtin(name):
return getattr(__builtins__, name)
So, this works as follows.
get_builtin(desired_type)(some_input_string)
This parses my string for desired_type='float' or 'int', saving me some hard-coding for some applications requiring string parsing.
When I define the get_builtin function directly into a python interpreter, this function behaves properly. However, when I try to call this function via ascii_tools.get_builtin('float'), I get the following error:
AttributeError: 'dict' object has no attribute 'float'
Same error occurs for 'int' input. This seemed strange, since the method works just fine when defined in the global namespace of the interpreter. So I checked the output of pdb, which is:
NameError: name 'ascii_tools' is not defined
This must have something to do with the namespace of builtins. Maybe calls to builtins somehow check the namespace of the module the call is nested in, and this is causing the problem?