首页 > 编程语言 > 详细

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

时间:2017-09-09 18:48:11      阅读:355      评论:0      收藏:0      [点我收藏+]

例:

#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

 

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

原文:http://www.cnblogs.com/liuliu-word/p/7498810.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!