• A decorator is a callable that takes another function as argument (the decorated function). The decorator may perform some processing with the decorated function, and returns it or replaces it with another function or callable object.

1. Variable Scope Rules

b = 6
def f1(a):
print(a) # 3
print(b) # 6
f1(3)
b = 6
def f2(a):
print(a) # 3
print(b) # UnboundLocalError: local variable 'b' referenced before assignment
b = 9
f2(3) # 1. When Python compiles the body of the function, it decides that b is a local
# variable because it's assigned within the function.
# 2. Python does not require you to declare variables, but assumes that a variable
# assigned in the body of a function is local
b = 6
def f3(a):
global b # declare
print(a) # 3
print(b) # 6
b = 9
f3(3)

2. Closures

  • Actually, a closure is a function with an extended scope that encompasses nonglobal variables referenced in the body of the function but not defined there. It does not matter whether the function is anonymous or not; what matters is that it can access nonglobal variables that are defined outside of its body.
  • A closure is a function that retains the bindings of the free variables that exist when the function is defined, so that they can be used later when the function is invoked and the defining scope is no longer available.
  • The only situation in which a function may need to deal with external variables that are nonglobal is when it is nested in another function.
def make_averager():
series = []
def averager(new_value):
series.append(new_value)
total = sum(series)
return total / len(series)
return averager avg = make_averager()
print avg(10) # 10
print avg(12) # 11
print avg(14) # 12
print avg.__code__.co_varnames # ('new_value', 'total')
print avg.__code__.co_freevars # ('series',)
print avg.__closure__ # (<cell at 0x1030d1280: list object at 0x1030a97e8>,)
print avg.__closure__[0].cell_contents # [10, 12, 14]

  • free variable: a variable that is not bound in the local scope.

3. The nonlocal Declaration

def make_averager():
count = 0
total = 0
def averager(new_value):
count += 1
total += new_value
return total / count
return averager avg = make_averager()
print avg(10) # UnboundLocalError: local variable 'count' referenced before assignment # 1. We actually assign count in the body of averager, and that makes it a local variable.
# 2. We didn't have this problem before, because we never assigned to the series name,
# we only called series.append and invoked sum and len on it.
# 3. With immutable types, if you try to rebind them, then you are implicitly creating a
# local variable, it's no longer a free variable, therefore it is not saved in the closure.
def make_averager():
count = 0
total = 0
def averager(new_value):
nonlocal count, total # **********
count += 1
total += new_value
return total / count
return averager avg = make_averager()
print(avg(10)) # 10.0 # 1. The nonlocal declaration was introduced in Python 3. It lets you flag a variable as a
# free variable even when it is assigned a new value within the function.

4. Implementing a Simple Decorator

  • Typical behavior of a decorator: It replaces the decorated function with a new function that accepts the same arguments and (usually) returns whatever the decorated function was supposed to return, while also doing some extra processing.
def deco(func):
print('running deco()')
print('func -> %s' % str(func))
def inner():
print('running inner()')
return inner @deco # target = deco(target)
def target():
print('running target()') target()
# running deco() # executed immediately when the module is loaded
# func -> <function target at 0x106c460c8>
# running inner()
print(target.__name__) # inner (function is replaced)
import time
FMT = '[{elapsed:0.8f}s] {name}({arg_str}) -> {result}'
def clock(func):
def clocked(*args, **kwargs):
t0 = time.time()
result = func(*args, **kwargs)
elapsed = time.time() - t0 # 用时
name = func.__name__
arg_lst = []
if args:
arg_lst.append(', '.join(repr(arg) for arg in args))
if kwargs:
pairs = ['%s=%r' % (k, w) for k, w in sorted(kwargs.items())]
arg_lst.append(', '.join(pairs))
arg_str = ', '.join(arg_lst)
print(FMT.format(**locals())) # any local variable of clocked
return result
return clocked @clock
def snooze(seconds):
time.sleep(seconds) @clock
def factorial(n):
return 1 if n < 2 else n*factorial(n-1) print('*' * 40, 'Calling snooze(.123)')
snooze(.123)
print('*' * 40, 'Calling factorial(6)')
print('6! =', factorial(6))
# ('****************************************', 'Calling snooze(.123)')
# [0.12694907s] snooze(0.123) -> None
# ('****************************************', 'Calling factorial(6)')
# [0.00000286s] factorial(1) -> 1
# [0.00004411s] factorial(2) -> 2
# [0.00005984s] factorial(3) -> 6
# [0.00007296s] factorial(4) -> 24
# [0.00014281s] factorial(5) -> 120
# [0.00015783s] factorial(6) -> 720
# ('6! =', 720)

5. Decorators in the Standard Library

5.1 functools.wraps

import functools
def deco(func):
@functools.wraps(func) # copy the relevant attributes from func to inner
def inner():
print('running inner()')
return inner @deco
def target():
"""i'm target"""
print('running target()') target() # running inner()
print(target.__name__) # target
print(target.__doc__) # i'm target

5.2 functools.lru_cache

  • An optimization technique that works by saving the results of previous invocations of an expensive function, avoiding repeat computations on previously used arguments.
  • The cache is limited by discarding the entries that have not been read for a while.
import functools
@functools.lru_cache() # Python 3
@clock
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-2) + fibonacci(n-1) print(fibonacci(6))
# [0.00000095s] fibonacci(0) -> 0
# [0.00000215s] fibonacci(1) -> 1
# [0.00036097s] fibonacci(2) -> 1
# [0.00000167s] fibonacci(3) -> 2
# [0.00039101s] fibonacci(4) -> 3
# [0.00000095s] fibonacci(5) -> 5
# [0.00041699s] fibonacci(6) -> 8
# 8 # @functools.lru_cache(maxsize=128, typed=False)
# maxsize: how many call results are stored. (should be a power of 2)
# typed: True -> store results of different argument types separately (1 / 1.0)
# False -> don't store # [notes]: lru_cache uses a dict to store the results, all the arguments
# taken by the decorated function must be hashable.

5.3 functools.singledispatch

  • The new functools.singledispatch decorator in Python 3.4 allows each module to contribute to the overall solution, and lets you easily provide a specialized function even for classes that you can’t edit.
  • If you decorate a plain function with @singledispatch, it becomes a generic function: a group of functions to perform the same operation in different ways, depending on the type of the first argument.
from functools import singledispatch
from collections import abc
import numbers
import html @singledispatch # mark the base function handles the obj type.
def htmlize(obj):
content = html.escape(repr(obj))
return '<pre>{}</pre>'.format(content) @htmlize.register(str) # @«base_function».register(«type»).
def _(text): # name of the specialized functions is irrelevant
return '<p>{0}</p>'.format(html.escape(text)) @htmlize.register(numbers.Integral)
def _(n):
return '<pre>{0} (0x{0:x})</pre>'.format(n) @htmlize.register(tuple)
@htmlize.register(abc.MutableSequence)
def _(seq):
inner = '</li>\n<li>'.join(htmlize(item) for item in seq)
return '<ul>\n<li>' + inner + '</li>\n</ul>' print(htmlize(str)) # <pre><class 'str'></pre>
print(htmlize('<hello>')) # <p><hello></p>
print(htmlize(10)) # <pre>10 (0xa)</pre>
print(htmlize(['alpha', {3, 2, 1}]))
# <ul>
# <li><p>alpha</p></li>
# <li><pre>{1, 2, 3}</pre></li>
# </ul> # 1. Register the specialized functions to handle ABCs (abstract classes)
# instead of concrete implementations like int and list. (support more)
# 2. A notable quality of the singledispatch mechanism is that you can
# register specialized functions anywhere in the system, in any module.

6. Stacked Decorators

@d1
@d2
def f():
print('f') # f = d1(d2(f))

7. Parameterized Decorators

  • Make a decorator factory that takes those arguments and returns a decorator, which is then applied to the function to be decorated.
def outter(a=1):
def deco(func):
def inner():
if a == 1:
print(1)
else:
print(a)
return inner
return deco @outter()
def target1():
pass @outter(9)
def target2():
pass target1() # 1
target2() # 9

7. Function Decorators and Closures的更多相关文章

  1. 《流畅的Python》Data Structures--第7章 colsure and decorator

    Function Decorators and Closures 装饰器是用于增强函数的行为,理解它,就必须先理解闭包. Python3引入关键字nonlocal,如果要理解闭包,就必须了解它的所有方 ...

  2. 浅入浅出Typescript Decorators

    临时起的兴趣,想写一篇关于ts decorator的文章,就花小半天整理了一下...  这东西,在ES2017里好像也有... 文档的话看这里. 因为临时,就没想写太多文字介绍,带少许文字说明直接开撸 ...

  3. JavaScript Decorators 的简单理解

    Decorators,装饰器的意思, 所谓装饰就是对一个物件进行美化,让它变得更漂亮.最直观的例子就是房屋装修.你买了一套房子,但是毛坯房,你肯定不想住,那就对它装饰一下,床,桌子,电视,冰箱等一通买 ...

  4. TypeScript学习笔记(九):装饰器(Decorators)

    装饰器简介 装饰器(Decorators)为我们在类的声明及成员上通过元编程语法添加标注提供了一种方式. 需要注意的是:装饰器是一项实验性特性,在未来的版本中可能会发生改变. 若要启用实验性的装饰器特 ...

  5. 《dive into python3》 笔记摘录

    1.list can hold  arbitrary  objects and can expand dynamically as new items are added. A list is an  ...

  6. JAVASCRIPT的一些知识点梳理

    春节闲点,可以安心的梳理一下以前不是很清楚的东东.. 看的是以下几个URL: http://web.jobbole.com/82520/ http://blog.csdn.net/luoweifu/a ...

  7. [TypeScript] Reflection and Decorator Metadata

    TypeScript allows you to emit decorator metadata which enables more powerful features through reflec ...

  8. python函数与方法装饰器

    之前用python简单写了一下斐波那契数列的递归实现(如下),发现运行速度很慢. def fib_direct(n): assert n > 0, 'invalid n' if n < 3 ...

  9. 是什么让javascript变得如此奇妙

    What Makes Javascript Weird...and AWESOME -> First Class Functions -> Event-Driven Evironment ...

随机推荐

  1. 基于LSTM + keras 的诗歌生成器

        最近在github 上发现了一个好玩的项目,一个基于LSTM + keras 实现的诗歌生成器,地址是:https://github.com/youyuge34/Poems_generator ...

  2. useJDBC4ColumnNameAndLabelSemantics设置后无效,怎么办?

    连接的是DB2数据库, 在查询语句中有SELECT COLUMNNAME AS ALIASNAME FROM TABLE这样的结构时, 会报如下错误: Caused by: com.ibm.db2.j ...

  3. 自助机dmv?鸡肋

    今天终于扛着懒癌去了一趟所谓的dmv自动机(dmv now kiosk),发现此机器只处理有关vehicle的三种事项,比如vehicle的new registration之类的,如果是其它事情还是得 ...

  4. HIVE配置mysql metastore

    HIVE配置mysql metastore    hive中除了保存真正的数据以外还要额外保存用来描述库.表.数据的数据,称为hive的元数据.这些元数据又存放在何处呢?    如果不修改配置hive ...

  5. 第34课.数组操作符的重载("[]"重载)

    1.问题:string类对象还具备c方式字符串的灵活性吗?还能直接访问单个字符吗? 答案:可以按照c字符串的方式使用string对象 string s = "a1b2c3d4e"; ...

  6. CREATE TABLE——数据定义语言 (Data Definition Language, DDL)

    Sql语句分为三大类: 数据定义语言,负责创建,修改,删除表,索引和视图等对象: 数据操作语言,负责数据库中数据的插入,查询,删除等操作: 数据控制语言,用来授予和撤销用户权限. 数据定义语言 (Da ...

  7. pymysql连接和操作Mysql数据库

    pymysql 一.概要 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库, 二.PyMySQL 安装 pip install pymysql 三.操作流程 创建c ...

  8. CodeBlocks 配置

    CodeBlocks 配置 Code::Blocks 17.12 时间:2019.6 下载网址 http://www.codeblocks.org/downloads/26 ,这里选择的是 mingw ...

  9. 剑指offer12:求解double类型的浮点数base和int类型的整数exponent的次方。 保证base和exponent不同时为0

    1. 题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.保证base和exponent不同时为0. 2. 思路和方法 分析: 由于 ...

  10. python之并发编程(概念篇)

    一.进程 1.什么是进程 进程是正在进行的一个过程或者一个任务.而负责执行任务的则是cpu. 2.进程与程序的区别 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程 ...