python 根据字符串语句进行操作再造函数(evec和eval方法)

例:

#coding:utf-8
'''
Created on 2017年9月9日

@author: Bss
'''
test_list=['def','a','']
test_list1=['print','hello']
exec "print 'hello'"
a=test_list1[0]
b=test_list1[1]
c=a+"'"+b+"'"
print a,b,c
exec c
c="print 'hello'"
exec c

执行结果:

#coding:utf-8
'''
Created on 2017年9月9日

@author: Bss
'''
test_list=['def','a','']
test_list1=['print','hello']
exec "print 'hello'"
a=test_list1[0]
b=test_list1[1]
c=a+"'"+b+"'"
print a,b,c
exec c
c="print 'hello'"
exec c

Python 3.0中,exec是一个函数不是一个语句了,因此使用exec('字符串语句')的方式来调用

exec "print  'hello'"执行命令打印出hello

原文地址:https://www.cnblogs.com/liuliu-word/p/7498810.html