python的compile函數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!usr/bin/env python
#coding:utf-8
  
namespace = {'name':'wupeiqi','data':[18,73,84]}
  
code =  '''def hellocute():return  "name %s ,age %d" %(name,data[0],) '''
  
func = compile(code, '<string>'"exec")
  
exec func in namespace
  
result = namespace['hellocute']()
  
print result
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章