Python功能之反射
有如下文件:
index.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'ryan'
"""
import home
print 'oldboy....'
url=raw_input('url:')
if url == 'home/dev':
ret = home.dev()
print ret
if url =='/home/index':
ret = home.index()
print ret
if url =='/home/user':
ret = home.user
print ret
if url == '/home/passwd':
ret = home.passwd
print ret
else:
print '404'
"""
#getattr、setattr、delattr、 hasattr
#找到home文件,将内容加载到内存,然后getattr、setattr、delattr、 hasattr 对home中断函数进行操作
#由于函数在没有执行之前,只是将函数名放入内存
import home
#print dir(home)
#print hasattr(home,'dev')#通过函数hasattr到内存中找到home模块,判断home模块下是否存在dev函数,
#有返回True,否则返回false
#print dir(home)
#print getattr(home,'dev')#到内存中的home模块中去获取函数dev(其实时获取dev函数在内存中的地址)
#print setattr(home,'alex','gentle man')#通过setattr对内存中的home模块添加一个字符串alex
#print dir(home)
#delattr(home,'dev')#通过delattr删除内存中home模块中的dev函数
#print dir(home)
#
"""
class Foo:
static_name='nba'
def __init__(self):
self.name='alex'
def show(self):
pass
@staticmethod
def static_show(self):
pass
@claSSMethod
def class_show(cls):
pass
#obj=Foo()
#print Foo.__dict__.keys() #查看类里面的成员
#print hasattr(Foo,'static_show')
#print obj.__dict__#查看对象里有哪些成员
#print hasattr(obj,'name')
#print hasattr(obj,'show')#普通方法是存储在类里,
#但是这里用hasattr函数在对象obj中查找是否存在show方法,
#返回了Ture,原因是在对象obj中没有找到之后会通过对象指针到创建对象的类中进一步查找,
#所以这里返回True
"""
"""
print hasattr(Foo,'__init__')
print "##########分隔符①################"
print hasattr(Foo,'static_name')
print "##########分隔符②################"
print hasattr(Foo,'show')
print "##########分隔符③################"
print hasattr(Foo,'static_show')
print "##########分隔符④################"
print hasattr(Foo,'class_show')
obj=Foo()
print obj.__dict__
print hasattr(obj,'show')
print hasattr(obj,'__init__')
print "##########分隔符①################"
print hasattr(obj,'static_name')
print "##########分隔符②################"
print hasattr(obj,'show')
print "##########分隔符③################"
print hasattr(obj,'static_show')
print "##########分隔符④################"
print hasattr(obj,'class_show')
"""
"""
import home
cls = getattr(home,"Foo")
print cls
s_name = getattr(cls,'static_name')
print s_name
c_show = getattr(cls,'show')
print c_show
s_show=getattr(cls,'static_show')
print s_show
cla_show = getattr(cls,'class_show')
print cla_show
"""
import home
cls = getattr(cls,'Foo')
obj=cls()#实例化一个对象obj
name = getattr(obj,'name')
print name
home.py
#!/usr/bin/env Python
# -*- coding:utf-8 -*-
__author__ = 'ryan'
def index():
return 'result home.index'
def dev():
return 'result home.dev'
def user():
return 'result home.user'
def passwd():
return 'result home.passwd'
"""
class Foo:
static_name='nba'
def __init__(self):
self.name='alex'
def show(self):
pass
@staticmethod
def static_show(self):
pass
@classmethod
def class_show(cls):
pass
"""
反射
fanshe.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'ryan'
url = raw_input('url:')
controller,action =url.split('/')
import home
#action动作是一个字符串,去某个容器(模块)中去找函数,字符串即为函数名,
#如果有则获取函数
func =getattr(home,action)
func = dev dev1():
return 'result home.dev'
ret = func()
#func=getattr(home,'index')
ret=func()
print ret
webdemo.py
#!/usr/bin/env python
#coding:utf-8
from wsgiref.simple_server import make_server
def RunServer(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
url = environ['PATH_INFO']
temp = url.split('/')[1]
import home
#去home模块中检查,是否含有指定的函数
is_exist = hasattr(home, temp)
#如果存在指定的函数
if is_exist:
#获取函数
func = getattr(home, temp)
#执行函数并获取返回值
ret = func()
#将函数返回值响应响应给请求者,即浏览器
return ret
else:
#如果不存在函数则返回给浏览器'404 not found'
return '404 not found'
if __name__ == '__main__':
Httpd = make_server('', 8001, RunServer)
print "Serving HTTP on port 8001..."
httpd.serve_forever()
相关文章