If I make a code like:
lists = ["a='1'", "b='2'", "c=a+b"] returned_list = [] for x in lists: exec(x) print(c) It works, and It print "12". but, If I use exec() in function:
lists = ["a='1'", "b='2'", "c=a+b"] def test(lst): for x in lists: exec(x) print(c) test(lists) It returns NameError: name 'c' is not defined. How could I use exec() in function?
没有评论:
发表评论