標籤:python import
有如下檔案:
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-8from wsgiref.simple_server import make_serverdef 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()
本文出自 “平平淡淡才是真” 部落格,請務必保留此出處http://ucode.blog.51cto.com/10837891/1763328
Python功能之反射