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

Python装饰器有趣实例探究

时间:2015-06-05 00:44:47      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

廖老师的教程实在太高深,没弄懂,点击打开链接

def deco_functionNeedDoc(func):
    if func.__doc__ == None :
        print func, "has no __doc__, it's a bad habit."
    else:
        print func, ':', func.__doc__, '.'
    return func
@deco_functionNeedDoc
def f():
    print 'f() Do something'
@deco_functionNeedDoc
def g():
    'I have a __doc__'
    print 'g() Do something'
f()
g()
print f
print g

这段代码打印结果如下:

<function f at 0x0238F930> has nodoc, it‘s a bad habit.

<function g at 0x0238F8B0> : I have a doc .f() Do somethingg() Do something

<function f at 0x0238F930>

<function g at 0x0238F8B0>

当时我就晕菜了,想了很久,原来在@装饰器函数的时候就会调用装饰器,装饰器函数return func,而func就是传进去的参数f。这个时候把代码改改。


def deco_functionNeedDoc(func):
    if func.__doc__ == None :
        print func, "has no __doc__, it's a bad habit."
    else:
        print func, ':', func.__doc__, '.'
    def func_1(*args, **kw):
        print func.__name__,' : this is func_1'
    return func_1
@deco_functionNeedDoc
def f():
    print 'f() Do something'
@deco_functionNeedDoc
def g():
    'I have a __doc__'
    print 'g() Do something'
f()
g()
print f
print g

此时的打印结果:

<function f at 0x023CF930> has no __doc__, it‘s a bad habit.
<function g at 0x023CF8B0> : I have a __doc__ .
f  : this is func_1
g  : this is func_1
func_1
func_1


问题至此,应该很明了了~~只是,装饰器拿来干嘛呢??应用在什么情况下呢??待探索


最后再贴一个超级大团圆,太有意思了,这个不解释了,困了,理解了这个,估摸装饰器的基本原理就透彻了。

def deco_functionNeedDoc(func1):
    if func1.__name__ == "yyy" :
        print func1, "the func1 == yyy"
    else:
        print func1, ':', func1.__doc__, '.'

    def xxx() : # y + f + h + x
        func1() # y + f + h + f + h
        print "plus xxx"
        return func1() # y + f + h + f + h

    return xxx

def deco_functionNeedDocxxx(func2):
    if func2.__name__ == "hhh" :
        print func2, "the func2 == hhh"
    else:
        print func2, ':', func2.__doc__, '.'

    def yyy(): # y + f + h + f + h
        print "plus yyy"
        func2()  # f + h
        return func2() # f + h

    return yyy

def deco_functionNeedDochhh(func3):
    if func3.__name__ == "f" :
        print func3, "the func3 == f"
    else:
        print func3, ':', func3.__doc__, '.'

    def hhh() : # = f + h
        func3()  # = f
        print "plus hhh"
        return "Hello Wrold"
    return hhh


@deco_functionNeedDoc
@deco_functionNeedDocxxx
@deco_functionNeedDochhh
def f():
    print 'print original fff'

f()
print "------------"
print f
print "------------"
print f()

结果如下:

<function f at 0x109a2b938> the func3 == f
<function hhh at 0x109a2b9b0> the func2 == hhh
<function yyy at 0x109a2ba28> the func1 == yyy
plus yyy
print original fff
plus hhh
print original fff
plus hhh
plus xxx
plus yyy
print original fff
plus hhh
print original fff
plus hhh
------------
<function xxx at 0x109a2baa0>
------------
plus yyy
print original fff
plus hhh
print original fff
plus hhh
plus xxx
plus yyy
print original fff
plus hhh
print original fff
plus hhh
Hello Wrold

Python装饰器有趣实例探究

标签:

原文地址:http://blog.csdn.net/sophie2805/article/details/46369315

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