[Python]从装饰器说开去


def deco(func):
print "deco"
return func
@deco
def foo():
return "hello"
#main
if __name__=="__main__":
print foo()

装饰器理解起来就是

def deco(func):
print "deco"
return func

def foo():
return "hello"
#main
if __name__=="__main__":
fun = deco(foo)
print fun()

但是装饰器有个重要的特性就是在装饰的时候会调用一次

def deco(func):
print "deco"
return func
@deco
def foo():
return "hello"
#main
if __name__=="__main__":
pass

理解带参数的装饰器

def deco(**kw):
print kw
def _deco(func):
return func
return _deco
@deco(key="123")
def foo():
return "hello"
#main
if __name__=="__main__":
print foo()

理解起来就是

def deco(**kw):
print kw
def _deco(func):
return func
return _deco
def foo():
return "hello"
#main
if __name__=="__main__":
#print deco(key='123')(foo)()
fun = deco(key='123')(foo)
print fun()

利用装饰器在装饰时会调用一次,我们可以利用它在程序真正开始执行之前注册函数,以便方便的在需要的时候调用,实现逻辑解耦,举个栗子!

mapper={}

def deco(**kw):
key = kw['key']
def _deco(func):
#如没有注册到mapper中就把该函数注册进mapper
if key not in mapper:
mapper[key] = func
return func
return _deco

@deco(key='get')
def foo1():
return "call get"

@deco(key='post')
def foo2():
return "call post"

#main
if __name__=="__main__":
key = 'get'
#查找key=get的函数
call = mapper[key]
#调用这个函数
print call()

这样就形成了一个类似框架的东西,仔细观察的话,很像springMVC或者bottle,下一章节,我们就来用装饰器来封装一下python的web处理,服务器采用tornado,打算实现的最终目标如下:

@Router.route(url=r"hello/([a-z]+)",method=Router._GET|Router._POST)
def test(self,req,who):
#http://localhost:8888/hello/billy
return "Hi,"+who

@Router.route(url=r"greetings/([a-z]+)",method=Router._GET)
def test2(self,req,who):
#http://localhost:8888/greetings/rowland
raise Exception("error")

@Router.route(url=r"book/([a-z]+)/(\d+)",method=Router._GET|Router._POST)
def test3(self,req,categories,bookid):
#http://localhost:8888/book/medicine/49875
return "You are looking for a "+categories+" book\n"+"book No. " + bookid

@Router.route(url=r"json",method=Router._POST)
def test4(self,req):
#http://localhost:8888/json
#print req.request.body
who=req.json_args.get("who","default")
age=req.json_args.get("age",0)
person={}
person['who']=who
person['age']=int(age)
return person
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值