---
title: "python的decorator@"
author: "splayx"
date: "2013-05-14T19:44:46Z"
tags: []
categories: ["编程语言"]
slug: "python-decorator"
draft: false
lastmod: "2013-05-14T19:46:16Z"
id: 1869425
url: "https://splayx.iteye.com/blog/1869425"
---

decorator是python的一个特性。

decorator可以是函数也可以是一个类。

 

decorator@的用处：

1）在函数开始前做一些初始工作，例如参数校验，调用统计等。

2）在函数结束后做一些收尾工作。

3）类似C++模板的功能。

 

**decorator是函数时，**

```
def decorator(fun):
    def call(*args, **kwargs):
        print "do something before fun!"
        if len(args) < 1:
            print "args is less"
        else:
            fun(args[0])
        print "do something after fun!"
    return call

@decorator
def hello(a):
    print "hello"

# hello(*args, **kwargs)
# is equivalent to
# decorator(hello)(*args, **kwargs)
# see the example for more detail

print "hello() is called:"
hello()

print "-----------------------------"

print "hello(1) is called:"
hello(1)


# decorator chain

def chain0(fun):
    def call(*args0, **kwargs0):
        print "chain0"
        fun(*args0, **kwargs0)
    return call


def chain1(fun):
    def call(*args1, **kwargs1):
        print "chain1"
        fun(*args1, **kwargs1)
    return call

@chain0
@chain1
def chain2(*args2, **kwargs2):
    print "chain2"

# attention!!
# chain2(*args0, **kwargs0)
# is equivalent to chain0(chain1(chain2))(*args0, **kwargs0)

# it is not chain2(*args2, **kwargs2)
# it is not chain0(chain1(chain2(*args0, **kwargs0)))
# it is not chain0(chain1(chain2(*args2, **kwargs2)))

chain2()
```

 

运行结果：

hello() is called:

do something before fun!

args is less

do something after fun!

\-----------------------------

hello(1) is called:

do something before fun!

hello

do something after fun!

chain0

chain1

chain2

 

具体原因看注释就明白了。

 

**decorator是类时，**

 

```
class decorator(object):
    def __init__(self, func):
        print func
        self.func = func
        print '__init__'

    def __call__(self, *args, **kwargs):
        print 'obj = %s, cls = %s' % (self.obj, self.cls)
        return self.func.__call__(*args, **kwargs)

    def __get__(self, obj, cls):
        self.obj = obj 
        self.cls = cls 
        print '__get__'
        return self.__call__

class foo(object):
    @decorator
    def hello(self):
        print 'hello'

    @decorator
    def world(self):
        print 'world'


tmp = foo()
print '-------------------------'
tmp.hello(tmp)
tmp.world(tmp)
```

 

 

 

运行结果：

 

&lt;function hello at 0x2b978ba2d578&gt;

\_\_init\__

&lt;function world at 0x2b978ba2d5f0&gt;

\_\_init\__

\-------------------------

\_\_get\__

obj = &lt;\_\_main\_\_.foo object at 0x2b978f03a710&gt;, cls = &lt;class '\_\_main\_\_.foo'&gt;

hello

\_\_get\__

obj = &lt;\_\_main\_\_.foo object at 0x2b978f03a710&gt;, cls = &lt;class '\_\_main\_\_.foo'&gt;

world

 

可以看出在创建foo实例的时候，

foo中的函数如果有对应的类decorator，

那么类decorator也会分别做相应的初始化。

\_\_get\_\_之后返回一个函数，然后就跟前面的函数decorator一样了。