python - Calling a function using exec -
trying call function string using exec doesn't work. attached simple example code below.
i error square_it() missing 1 required positional argument: 'num.' know missing don't know how have argument in globals syntax.
below 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()
the error isn't happening in exec call; it's happening in first line, when call globals. rest of code irrelevant.
calling function there exactly same doing other way; have calling parentheses, need put argument in there:
globals()['square_it'](2)
Comments
Post a Comment