Python 反射作用

反射 代码示例:
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')



posted @ 2016-04-08 14:12  FreeMan1  阅读(158)  评论(0编辑  收藏  举报