Sep-27-2023, 06:34 AM
hi
the below code is in https://realpython.com/python-eval-function/ :
what is the problem?
thanks
the below code is in https://realpython.com/python-eval-function/ :
def eval_expression(input_string): # Step 1 allowed_names = {"sum": sum} # Step 2 code = compile(input_string, "<string>", "eval") # Step 3 for name in code.co_names: if name not in allowed_names: # Step 4 raise NameError(f"Use of {name} not allowed") return eval(code, {"__builtins__": {}}, allowed_names) eval_expression("pow(10, 2)")when I run it in idle, my output is :
Error:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 10, in eval_expression
NameError: Use of pow not allowed
raise NameError(f"Use of {name} not allowed")
NameError: Use of pow not allowed
in above output, in the 5'th line the{name} must be the pow as it is in the output of the site.what is the problem?
thanks