⽣成器(Generators)

⾸先我们要理解迭代器(iterators)。根据维基百科,迭代器是⼀个让程序员可以遍历⼀个容 器(特别是列表)的对象。然⽽,⼀个迭代器在遍历并读取⼀个容器的数据元素时,并不 会执⾏⼀个迭代。你可能有点晕了,那我们来个慢动作。换句话说这⾥有三个部分:

  • 可迭代对象(Iterable)
  • 迭代器(Iterator)
  • 迭代(Iteration)

上⾯这些部分互相联系。我们会先各个击破来讨论他们,然后再讨论⽣成器

可迭代对象(Iterable)

Python 中任意的对象,只要它定义了可以返回⼀个迭代器的 __iter__ ⽅法,或者定义了 可以⽀持下标索引的__getitem__⽅法(这些双下划线⽅法会在其他章节中全⾯解释), 那么它就是⼀个可迭代对象。简单说,可迭代对象就是能提供迭代器的任意对象。那迭代 器又是什么呢?

迭代器(Iterator)

任意对象,只要定义了 next(Python2) 或者__next__⽅法,它就是⼀个迭代器。就这么简单。现在我们来理解迭代(iteration)

迭代(Iteration)

⽤简单的话讲,它就是从某个地⽅(⽐如⼀个列表)取出⼀个元素的过程。当我们使⽤⼀ 个循环来遍历某个东西时,这个过程本⾝就叫迭代。现在既然我们有了这些术语的基本理解

⽣成器(Generators)

⽣成器也是⼀种迭代器,但是你只能对其迭代⼀次。这是因为它们并没有把所有的值存在内存中,⽽是在运⾏时⽣成值。你通过遍历来使⽤它们,要么⽤⼀个“for”循环,要么将它们传递给任意可以进⾏迭代的函数和结构。⼤多数时候⽣成器是以函数来实现的。然⽽,它们并不返回⼀个值,⽽是 yield(暂且译作“⽣出”)⼀个值。这⾥有个⽣成器函数的简单 例⼦:

def generator_function():
    for i in range(10):
        yield i
for item in generator_function():
print(item)
# Output: 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9

这个案例并不是⾮常实⽤。⽣成器最佳应⽤场景是:你不想同⼀时间将所有计算出来的⼤ 量结果集分配到内存当中,特别是结果集⾥还包含循环。 译者注:这样做会消耗⼤量资源 许多 Python 2 ⾥的标准库函数都会返回列表,⽽ Python 3 都修改成了返回⽣成器,因为⽣成 器占⽤更少的资源。

下⾯是⼀个计算斐波那契数列的⽣成器:

# generator version
def fibon(n):
    a = b = 1
    for i in range(n):
        yield a
        a, b = b, a + b
Now we can use it like this:
for x in fibon(1000000):
    print(x)

⽤这种⽅式,我们可以不⽤担⼼它会使⽤⼤量资源。然⽽,之前如果我们这样来实现的 话:

def fibon(n):
    a = b = 1
    result = []
    for i in range(n):
        result.append(a)
        a, b = b, a + b
    return result

这也许会在计算很⼤的输⼊参数时,⽤尽所有的资源。我们已经讨论过⽣成器使⽤⼀次迭 代,但我们并没有测试过。在测试前你需要再知道⼀个 Python 内置函数:next()。它允 许我们获取⼀个序列的下⼀个元素。那我们来验证下我们的理解:

def generator_function():
    for i in range(3):
        yield i
gen = generator_function()
print(next(gen))
# Output: 0 print(next(gen))
# Output: 1 print(next(gen))
# Output: 2 print(next(gen))
# Output: Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# StopIteration

我们可以看到,在 yield 掉所有的值后,next()触发了⼀个 StopIteration 的异常。 基本上这个异常告诉我们,所有的值都已经被 yield 完了。你也许会奇怪,为什么我们在 使⽤ for 循环时没有这个异常呢?啊哈,答案很简单。for 循环会⾃动捕捉到这个异常并 停⽌调⽤ next()。你知不知道 Python 中⼀些内置数据类型也⽀持迭代哦?我们这就去看 看:

my_string = "Yasoob"
next(my_string)
# Output: Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: str object is not an iterator

好吧,这不是我们预期的。这个异常说那个 str 对象不是⼀个迭代器。对,就是这样!它 是⼀个可迭代对象,⽽不是⼀个迭代器。这意味着它⽀持迭代,但我们不能直接对其进⾏ 迭代操作。那我们怎样才能对它实施迭代呢?是时候学习下另⼀个内置函数,iter。它 将根据⼀个可迭代对象返回⼀个迭代器对象。这⾥是我们如何使⽤它:

my_string = "Yasoob"
my_iter = iter(my_string)
next(my_iter)
# Output: 'Y'

现在好多啦。我肯定你已经爱上了学习⽣成器。⼀定要记住,想要完全掌握这个概念,你 只有使⽤它。确保你按照这个模式,并在⽣成器对你有意义的任何时候都使⽤它。你绝对 不会失望的!

装饰器

装饰器(Decorators)是 Python 的⼀个重要部分。简单地说:他们是修改其他函数的功能的函 数。他们有助于让我们的代码更简短,也更 Pythonic(Python 范⼉)。⼤多数初学者不知 道在哪⼉使⽤它们,所以我将要分享下,哪些区域⾥装饰器可以让你的代码更简洁。 ⾸先,让我们讨论下如何写你⾃⼰的装饰器。 这可能是最难掌握的概念之⼀。我们会每次只讨论⼀个步骤,这样你能完全理解它。

⼀切皆对象

⾸先我们来理解下 Python 中的函数

def hi(name="yasoob"):
    return "hi " + name
print(hi())
# output: 'hi yasoob'
# 我们甚⾄可以将⼀个函数赋值给⼀个变量,⽐如 greet = hi
# 我们这⾥没有在使⽤⼩括号,因为我们并不是在调⽤hi函数
# ⽽是在将它放在greet变量⾥头。我们尝试运⾏下这个 print(greet())
# output: 'hi yasoob'
# 如果我们删掉旧的hi函数,看看会发⽣什么!
del hi
print(hi())
#outputs: NameError print(greet())
#outputs: 'hi yasoob'

在函数中定义函数

刚才那些就是函数的基本知识了。我们来让你的知识更进⼀步。在 Python 中我们可以在⼀ 个函数中定义另⼀个函数:

def hi(name="yasoob"):
    print("now you are inside the hi() function")
    def greet():
        return "now you are in the greet() function"
    def welcome():
        return "now you are in the welcome() function"
    print(greet()) print(welcome())
    print("now you are back in the hi() function")
hi()
#output:now you are inside the hi() function # now you are in the greet() function
# now you are in the welcome() function
# now you are back in the hi() function
# 上⾯展示了⽆论何时你调⽤hi(), greet()和welcome()将会同时被调⽤。
# 然后greet()和welcome()函数在hi()函数之外是不能访问的,⽐如:
greet() #outputs: NameError: name 'greet' is not defined

那现在我们知道了可以在函数中定义另外的函数。也就是说:我们可以创建嵌套的函数。 现在你需要再多学⼀点,就是函数也能返回函数。

从函数中返回函数

其实并不需要在⼀个函数⾥去执⾏另⼀个函数,我们也可以将其作为输出返回出来:

def hi(name="yasoob"):
    def greet():
        return "now you are in the greet() function"

    def welcome():
        return "now you are in the welcome() function"
    if name == "yasoob":
        return greet
    else:
        return welcome
a = hi()
print(a)
#outputs: <function greet at 0x7f2143c01500>
#上⾯清晰地展示了`a`现在指向到hi()函数中的greet()函数
#现在试试这个 print(a())
#outputs: now you are in the greet() function

再次看看这个代码。在 if/else 语句中我们返回 greet 和 welcome,⽽不是 greet()和 welcome()。为什么那样?这是因为当你把⼀对⼩括号放在后⾯,这个函数就会执⾏; 然⽽如果你不放括号在它后⾯,那它可以被到处传递,并且可以赋值给别的变量⽽不去执 ⾏它。 你明⽩了吗?让我再稍微多解释点细节。 当我们写下 a = hi(),hi()会被执⾏,⽽由于 name 参数默认是 yasoob,所以函 数 greet 被返回了。如果我们把语句改为 a = hi(name = "ali"),那么 welcome 函数 将被返回。我们还可以打印出 hi()(),这会输出 now you are in the greet() function。

将函数作为参数传给另⼀个函数

def hi():
    return "hi yasoob!"
def doSomethingBeforeHi(func):
    print("I am doing some boring work before executing hi()")
    print(func())
doSomethingBeforeHi(hi)
#outputs:I am doing some boring work before executing hi()
# hi yasoob!

现在你已经具备所有必需知识,来进⼀步学习装饰器真正是什么了。装饰器让你在⼀个函 数的前后去执⾏代码。

你的第⼀个装饰器

在上⼀个例⼦⾥,其实我们已经创建了⼀个装饰器!现在我们修改下上⼀个装饰器,并编 写⼀个稍微更有⽤点的程序:

def a_new_decorator(a_func):
    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()"
        a_func()
        print("I am doing some boring work after executing a_func()"
    return wrapTheFunction

def a_function_requiring_decoration():
    print("I am the function which needs some decoration to remove my foul smell"

a_function_requiring_decoration()
#outputs: "I am the function which needs some decoration to remove my foul smell"

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
#now a_function_requiring_decoration is wrapped by wrapTheFunction()

a_function_requiring_decoration()
#outputs:I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()

你看明⽩了吗?我们刚刚应⽤了之前学习到的原理。这正是 python 中装饰器做的事情!它 们封装⼀个函数,并且⽤这样或者那样的⽅式来修改它的⾏为。现在你也许疑惑,我们在 代码⾥并没有使⽤@符号?那只是⼀个简短的⽅式来⽣成⼀个被装饰的函数。这⾥是我们 如何使⽤@来运⾏之前的代码

@a_new_decorator
def a_function_requiring_decoration():
    """Hey you! Decorate me!"""
    print("I am the function which needs some decoration to "
          "remove my foul smell")

a_function_requiring_decoration()
#outputs: I am doing some boring work before executing a_func()
#         I am the function which needs some decoration to remove my foul smell
#         I am doing some boring work after executing a_func()

#the @a_new_decorator is just a short way of saying:
a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

希望你现在对 Python 装饰器的工作原理有一个基本的理解。如果我们运行如下代码会存在一个问题:

print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction

这并不是我们想要的!Ouput 输出应该是“a_function_requiring_decoration”。这里的函数被 warpTheFunction 替代了。它重写了我们函数的名字和注释文档(docstring)。幸运的是 Python 提供给我们一个简单的函数来解决这个问题,那就是 functools.wraps。我们修改上一个例子来使用 functools.wraps:

from functools import wraps

def a_new_decorator(a_func):
    @wraps(a_func)
    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()")
        a_func()
        print("I am doing some boring work after executing a_func()")
    return wrapTheFunction

@a_new_decorator
def a_function_requiring_decoration():
    """Hey yo! Decorate me!"""
    print("I am the function which needs some decoration to "
          "remove my foul smell")

print(a_function_requiring_decoration.__name__)
# Output: a_function_requiring_decoration

现在好多了。我们接下来学习装饰器的一些常用场景。

蓝本规范:

from functools import wraps
def decorator_name(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        if not can_run:
            return "Function will not run"
        return f(*args, **kwargs)
    return decorated

@decorator_name
def func():
    return("Function is running")

can_run = True
print(func())
# Output: Function is running

can_run = False
print(func())
# Output: Function will not run

注意:@wraps 接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。

使用场景

现在我们来看一下装饰器在哪些地方特别耀眼,以及使用它可以让一些事情管理起来变得更简单。

授权(Authorization)

装饰器能有助于检查某个人是否被授权去使用一个 web 应用的端点(endpoint)。它们被大量使用于 Flask 和 Django web 框架中。这里是一个例子来使用基于装饰器的授权:

from functools import wraps

def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            authenticate()
        return f(*args, **kwargs)
    return decorated

日志(Logging)

日志是装饰器运用的另一个亮点。这是个例子:

from functools import wraps

def logit(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print(func.__name__ + " was called")
        return func(*args, **kwargs)
    return with_logging

@logit
def addition_func(x):
   """Do some math."""
   return x + x


result = addition_func(4)
# Output: addition_func was called

我敢肯定你已经在思考装饰器的一个其他聪明用法了。

带参数的装饰器

来想想这个问题,难道@wraps 不也是个装饰器吗?但是,它接收一个参数,就像任何普通的函数能做的那样。那么,为什么我们不也那样做呢?

这是因为,当你使用@my_decorator 语法时,你是在应用一个以单个函数作为参数的一个包裹函数。记住,Python 里每个东西都是一个对象,而且这包括函数!记住了这些,我们可以编写一下能返回一个包裹函数的函数。

在函数中嵌入装饰器

我们回到日志的例子,并创建一个包裹函数,能让我们指定一个用于输出的日志文件。

from functools import wraps

def logit(logfile='out.log'):
    def logging_decorator(func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + " was called"
            print(log_string)
            # 打开logfile,并写入内容
            with open(logfile, 'a') as opened_file:
                # 现在将日志打到指定的logfile
                opened_file.write(log_string + '\n')
            return func(*args, **kwargs)
        return wrapped_function
    return logging_decorator

@logit()
def myfunc1():
    pass

myfunc1()
# Output: myfunc1 was called
# 现在一个叫做 out.log 的文件出现了,里面的内容就是上面的字符串

@logit(logfile='func2.log')
def myfunc2():
    pass

myfunc2()
# Output: myfunc2 was called
# 现在一个叫做 func2.log 的文件出现了,里面的内容就是上面的字符串

装饰器类

现在我们有了能用于正式环境的 logit 装饰器,但当我们的应用的某些部分还比较脆弱时,异常也许是需要更紧急关注的事情。比方说有时你只想打日志到一个文件。而有时你想把引起你注意的问题发送到一个 email,同时也保留日志,留个记录。这是一个使用继承的场景,但目前为止我们只看到过用来构建装饰器的函数。

幸运的是,类也可以用来构建装饰器。那我们现在以一个类而不是一个函数的方式,来重新构建 logit。

from functools import wraps

class logit(object):
    def __init__(self, logfile='out.log'):
        self.logfile = logfile

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + " was called"
            print(log_string)
            # 打开logfile并写入
            with open(self.logfile, 'a') as opened_file:
                # 现在将日志打到指定的文件
                opened_file.write(log_string + '\n')
            # 现在,发送一个通知
            self.notify()
            return func(*args, **kwargs)
        return wrapped_function

    def notify(self):
        # logit只打日志,不做别的
        pass

这个实现有一个附加优势,在于比嵌套函数的方式更加整洁,而且包裹一个函数还是使用跟以前一样的语法:

@logit()
def myfunc1():
    pass

现在,我们给 logit 创建子类,来添加 email 的功能(虽然 email 这个话题不会在这里展开)。

class email_logit(logit):
    '''
    一个logit的实现版本,可以在函数调用时发送email给管理员
    '''
    def __init__(self, email='admin@myproject.com', *args, **kwargs):
        self.email = email
        super(email_logit, self).__init__(*args, **kwargs)

    def notify(self):
        # 发送一封email到self.email
        # 这里就不做实现了
        pass

从现在起,@email_logit 将会和@logit 产生同样的效果,但是在打日志的基础上,还会多发送一封邮件给管理员。

by 李鹏