Trying to call a function as a string using exec but it doesn't work. I attached a simple example code below.
I get an error that square_it() is missing 1 required positional argument: 'num.' I know it is missing but I don't know how to have the argument in that globals syntax.
Below is an example:
def square_it(num):
result = num * num
return result
def test():
#code_globals = {}
globals()['square_it']()
code_locals = {'testing':0}
comd_str = "testing = square_it(2)"
exec(comd_str, globals(), code_locals)
print(code_locals['testing'])
test()
globals()['square_it']()because that is where the function is being called with no arguments — not via theexec()(execution never gets that far).