标签:
反射 代码示例:
person.py
def run (a):
print ‘running %s‘%a
def eat():
print ‘eating‘
def talk():
print ‘taliking‘
player.py
import person
def play(action):
return getattr(person,action)
action =play(‘run‘)
action(‘test‘)
action = play(‘eat‘)
action()
模块反射代码调用示例:
http_download.py
def download(a):
print ‘http download %s‘%a
def download2():
print ‘https download 2‘
https_download.py
def download(a):
print ‘http download %s‘%a
def download2():
print ‘https download 2‘
fanshe.py
import http_download
import https_download
def freeman_download(protocol):
download = __import__(protocol+"_download")
download.download(‘test‘)
#download.download2()
freeman_download(‘http‘)
freeman_download(‘https‘)
标签:
原文地址:http://www.cnblogs.com/yjz1/p/5365270.html