若要讓 exec 執行後的結果能被其它 scope 的程式存取, 要改用「exec CODE in globals()」或「exec CODE in locals(), globals()」, 見下面的例子:
def add_hello(): exec "def hello(): print 'hello'" hello() add_hello() # "hello" hello() # NameError: name 'hello' is not defined
def add_hello(): exec "def hello(): print 'hello'" in locals(), globals() hello() add_hello() # "hello" hello() # "hello"
Nice blog thanks to share this with us. people find many solutions here. students can learn easily from this article.
回覆刪除we are providing an all assignment service you will find all python assignment on our website.
python assignment help