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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -