美文网首页
【Bug】- python exec NameError: na

【Bug】- python exec NameError: na

作者: u14e | 来源:发表于2019-03-28 17:58 被阅读0次

exec 执行字符串代码:

code = """
{code}
result = run("{msg}")
"""

def exec_test():
    command = code.format(code="""
def upper(msg):
  return msg.upper()

def run(msg):
    print('start')
    return upper(msg)
    """, msg='u14e')
    exec(command, globals())
    # print(locals())
    print(result)

if __name__ == '__main__':
    exec_test()

参考链接:
- Using a function defined in an exec'ed string in Python 3 [duplicate]
- Python中的eval()、exec()及其相关函数

相关文章

网友评论

      本文标题:【Bug】- python exec NameError: na

      本文链接:https://www.haomeiwen.com/subject/yrycbqtx.html