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

Python 反射作用

时间:2016-04-08 14:54:22      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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



Python 反射作用

标签:

原文地址:http://www.cnblogs.com/yjz1/p/5365270.html

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