码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-09 18:58:58      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:eval   bsp   creat   执行命令   操作   style   auth   coding   方式   

例:

#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方法)

标签:eval   bsp   creat   执行命令   操作   style   auth   coding   方式   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!