How do I execute a string containing Python code in Python?

后端 未结 14 2416
一向
一向 2020-11-22 02:42

How do I execute a string containing Python code in Python?

14条回答
  •  失恋的感觉
    2020-11-22 03:06

    I tried quite a few things, but the only thing that work was the following:

    temp_dict = {}
    exec("temp_dict['val'] = 10") 
    print(temp_dict['val'])
    

    output:

    10

提交回复
热议问题