python3 装饰器_python3 装饰器-阿里云开发者社区

内嵌函数

def outer():

x = 1

def inner():

print('In inner')

print(x)

print(locals())

inner() # 若inner调用没有写在这里,inner函数仅仅被声明

b=outer()

outputs:

In inner

1

{'x': 1}

以上代码看起来有些复杂,但它仍是易于理解的。来看 #1 —— Python 搜索局部变量 x 失败,然后在属于另一个函数的外层作用域里寻找。变量 x 是函数 outer 的局部变量,但函数 inner 仍然有外层作用域的访问权限(至少有读和修改的权限)。在 #2 处调用函数 inner。值得注意的是,inner 在此处也只是一个变量名,遵循 Python 的变量查找规则——Python 首先在 outer 的作用域查找并找到了局部变量 inner。

函数是 Python 中的一级对象

在 Python 中有个常识:函数和其他任何东西一样,都是对象。函数包含变量,它并不那么特殊。

也许你从未考虑过函数可以有属性——但是函数在 Python 中,和其他任何东西一样都是对象。(如果对此感觉困惑,稍后你会看到 Python 中的类也是对象,和其他任何东西一样!)也许这有点学术的感觉——在 Python 中函数只是常规的值,就像其他任意类型的值一样。这意味着可以将函数当做实参传递给函数,或者在函数中将函数作为返回值返回。如果你从未想过这样使用,请看下面的可执行代码:

# 函数作为参数,因为在python中函数也是普通的对象,和类对象,变量一样

def add(x,y):

return x + y

def sub(x,y):

return x - y

def apply(func, x, y):

return func(x,y)

print(apply(add,1,5)) # 6

和其他变量一样,函数名就是变量标签.

再来看下面的例子:

def outer1():

def inner():

print('Inside inner')

return inner #1

f1=outer1() #2

f1()

outputs: Inside inner

这看起来也许有点怪异。在 #1 处返回一个其实是函数标签的变量 inner。也没有什么特殊语法——函数 outer 1返回了并没有被调用的函数 inner。还记得变量的生命周期吗?每次调用函数 outer 的时候,函数 inner 会被重新定义,但是如果函数 outer1 没有返回 inner,当 inner 超出 outer 的作用域,inner 的生命周期将结束。在 #2 处将获得返回值即函数 inner,并赋值给新变量 foo。

闭包

请参考专门的一片关于闭包的文章

>>>def outer(x):

... def inner():

... print x # 1

... return inner

>>>print1 = outer(1)

>>>print2 = outer(2)

>>>print1()

1

>>>print2()

2

装饰器

装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数。让我们从简单的开始,直到能写出实用的装饰器。

def outer2(some_func):

def inner():

print('before func:')

ret=some_func()

return ret + 1 #1

return inner # 注意不要带括号

def foo2():

return 1

decorated = outer2(foo2) # 注意是不带()的foo2 #2

print(decorated())

outputs:

before func:

2

请仔细看这个装饰器示例。首先,定义了一个带单个参数 some_func 的名为 outer 的函数。然后在 outer 内部定义了一个内嵌函数 inner。inner 函数将打印一行字符串然后调用 some_func,并在 #1 处获取其返回值。在每次 outer 被调用时,some_func 的值可能都会不同,但不论 some_func 是什么函数,都将调用它。最后,inner 返回 some_func() 的返回值加 1。在 #2 处可以看到,当调用赋值给 decorated 的返回函数时,得到的是一行文本输出和返回值 2。

我们可以说变量 decorated 是 foo 的装饰版——即 foo 加上一些东西。

class Coordinate:

def __init__(self,x,y):

self.x=x

self.y=y

def __repr__(self):

return 'Coord: ' + str(self.__dict__)

a=Coordinate(3,4)

print(a)

outputs: Coord: {'x': 3, 'y': 4}

接着改写

class Coordinate:

def __init__(self,x,y):

self.x=x

self.y=y

def __repr__(self):

return 'Coord: ' + str(self.__dict__)

# a=Coordinate(3,4)

# print(a)

def add2(a,b):

return Coordinate(a.x+b.x, a.y+b.y)

def sub2(a,b):

return Coordinate(a.x - b.x, a.y - b.y)

one = Coordinate(100, 200)

two = Coordinate(300, 200)

print(add2(one, two))

print(sub2(one, two))

# Coord: {'y': 400, 'x': 400}

# Coord: {'y': 0, 'x': -200}

函数装饰器 @ 符号的应用

@符号是装饰器的语法糖,在定义函数的时候使用

@bar

def foo():

print "foo"

等价于

def foo():

print "foo"

foo = bar(foo)

无参数装饰器

def foo(func):

print 'decorator foo'

return func

@foo

def bar():

print 'bar'

bar()

foo 函数被用作装饰器,其本身接收一个函数对象作为参数,然后做一些工作后,返回接收的参数,供外界调用。

注意: 时刻牢记 @foo 只是一个语法糖,其本质是 foo = bar(foo)

def logger(func):

def inner(*args, **kwargs): #1

print("Arguments were: %s, %s" % (args, kwargs))

return func(*args, **kwargs) #2

return inner

@logger

def foo1(x, y=1):

return x * y

@logger

def foo2():

return 2

rfoo1 = foo1(5, 4) #Arguments were: (5, 4), {

print(rfoo1) # 20

print(foo1(1))

# Arguments were: (1,), {}

# 1

带参数的装饰器

def use_logging(level):

def decorator(func):

def wrapper(*args,**kwargs):

if level == 'warn':

print('%s is running' % func.__name__)

return func(*args)

return wrapper

return decorator

@use_logging(level='warn')

def foo3(name='foo3'):

print('I am foo3')

foo3()

# foo3 is running

# I am foo3

@use_logging(level=’warn’) 调用的时候,python能够发现这一层的封装,并把参数传递到装饰器中

类装饰器

class Foo():

def __init__(self,func):

self._func=func

def __call__(self, *args, **kwargs):

print('class deco running')

self._func()

print('class deco ending')

@Foo

def bars():

print('Bars')

bars()

# class deco running

# Bars

# class deco ending

所有的函数都是可调用对象。

一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法call()。

class Person(object):

def __init__(self, name, gender):

self.name = name

self.gender = gender

def __call__(self, friend):

print ('My name is %s...' % self.name)

print ('My friend is %s...' % friend)

p = Person('Bob', 'male')

print(p('Tim'))

# My name is Bob...

# My friend is Tim...

简单来讲就是将动态输入的函数转成内嵌函数所需要表达的内容

# -*- coding:gbk -*-

'''示例6: 对参数数量不确定的函数进行装饰,

参数用(*args, **kwargs),自动适应变参和命名参数'''

def deco(func):

def _deco(*args, **kwargs):

print("before %s called." % func.__name__)

ret = func(*args, **kwargs)

print(" after %s called. result: %s" % (func.__name__, ret))

return ret

return _deco

@deco

def myfunc(a, b):

print(" myfunc(%s,%s) called." % (a, b))

return a + b

@deco

def myfunc2(a, b, c):

print(" myfunc2(%s,%s,%s) called." % (a, b, c))

return a + b + c

myfunc(1, 2)

myfunc(3, 4)

myfunc2(1, 2, 3)

myfunc2(3, 4, 5)

# before myfunc called.

# myfunc(1,2) called.

# after myfunc called. result: 3

# before myfunc called.

# myfunc(3,4) called.

# after myfunc called. result: 7

# before myfunc2 called.

# myfunc2(1,2,3) called.

# after myfunc2 called. result: 6

# before myfunc2 called.

# myfunc2(3,4,5) called.

# after myfunc2 called. result: 12

args 和 *kwargs

定义函数时使用 * 的用法意味着任何传递给函数的额外位置参数都是以 * 开头的

>>>def one(*args):

... print args # 1

>>>one()

()

>>>one(1, 2, 3)

(1, 2, 3)

>>>def two(x, y, *args): # 2

... print x, y, args

>>>two('a', 'b', 'c')

a b ('c',)

星* 符号也可以用在函数调用时,在这里它也有类似的意义。在调用函数时,以 * 开头的变量表示该变量内容需被取出用做位置参数。再举例如下:

>>>def add(x, y):

... return x + y

>>>lst = [1,2]

>>>add(lst[0], lst[1]) # 1

3

>>>add(*lst) # 2

3

在 #1 处的代码和 #2 处的作用相同——可以手动做的事情,在 #2 处 Python 帮我们自动处理了。这看起来不错,*args 可以表示在调用函数时从迭代器中取出位置参数, 也可以表示在定义函数时接收额外的位置参数。

接下来介绍稍微复杂一点的用来表示字典和键值对的 *,就像 用来表示迭代器和位置参数。

>>>def foo(**kwargs):

... print kwargs

>>>foo()

{}

>>>foo(x=1, y=2)

{'y': 2, 'x': 1}

当定义一个函数时,使用 kwargs 来表示所有未捕获的关键字参数将会被存储在字典 kwargs 中。此前 args 和 kwargs 都不是 Python 中语法的一部分,但在函数定义时使用这两个变量名是一种惯例。和 * 的使用一样,可以在函数调用和定义时使用。

>>>dct = {'x': 1, 'y': 2}

>>>def bar(x, y):

... return x + y

>>>bar(**dct)

调用顺序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值