《流畅的Python》读书笔记7(第五章:一等函数)

145 阅读6分钟

在Python中,函数是一等对象。编程语言理论家把“一等对象”定义为满足下述条件的程序实体:

  • 在运行时创建
  • 能赋值给变量或数据结构中的数据
  • 能作为参数传给函数
  • 能作为函数的返回结果

5.1 把函数视作对象

示例5-1 创建并测试一个函数,然后读取它的__doc__属性,再检测它的类型

>>> def factorial(n):

...     '''return n!'''

...     return 1 if n < 2 else n * factorial(n-1)

... 

>>> factorial(42)

1405006117752879898543142606244511569936384000000000

>>> factorial.__doc__

'return n!'

>>> type(factorial)

<class 'function'>

示例5-2 通过别的名称使用函数,再把函数作为参数传递

>>> fact = factorial

>>> fact

<function factorial at 0x7fba902daa60>

>>> fact(5)

120

>>> map(factorial, range(11))

<map object at 0x7fba902d8b38>

>>> list(map(fact, range(11)))

[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

5.2 高阶函数

接受函数为参数,或者把函数作为结果返回的函数就是高阶函数。

map函数就是一例,内置函数sorted也是:可选的key参数用于提供一个函数,它会应用到各个元素上进行排序。

示例5-3 根据单词长度给列表排序

>>> fruits = ['strawberry', 'fig', 'apple', 'cherry', 'raspberry', 'banana']

>>> sorted(fruits, key =len)

['fig', 'apple', 'cherry', 'banana', 'raspberry', 'strawberry']

示例5-4 根据反向拼写给一个单词列表排序

>>> def reverse(word):

...     return word[::-1]

... 

>>> reverse('testing')

'gnitset'

>>> sorted(fruits, key=reverse)

['banana', 'apple', 'fig', 'raspberry', 'strawberry', 'cherry']

map、filter和reduce的现代替代品

Python3中,map和filter还是内置函数,但是由于引入了列表推导和生成器表达式,它们变得没那么重要了。列表推导和生成器表达式具有了map和filter的功能,而且更容易阅读。

示例5-5 计算阶乘列表:map和filter与列表推导比较

>>> list(map(fact, range(6)))

[1, 1, 2, 6, 24, 120]

>>> [fact(n) for n in range(6)]

[1, 1, 2, 6, 24, 120]

>>> list(map(factorial, filter(lambda n:n%2, range(6))))

[1, 6, 120]

>>> [factorial(n) for n in range(6) if n%2]

[1, 6, 120]

5.3 匿名函数

lambda关键字在Python表达式内创建匿名函数。

lambda函数的定义体只能使用纯表达式。也就是,lambda函数的定义体中不能赋值,也不能使用while和try等Python语句。

>>> fruits = ['strawberry', 'fig', 'apple', 'cherry', 'raspberry', 'banana']

>>> sorted(fruits, key=lambda word:word[::-1])

['banana', 'apple', 'fig', 'raspberry', 'strawberry', 'cherry']

5.4 可调用对象

除了用户定义的函数,调用运算符()还可以应用到其他对象上。如果想判断对象能否调用,可以使用内置的callable()函数。

Python数据模型文档列出了9种可调用对象docs.python.org/3/reference…

>>> abs, str, 13

(<built-in function abs>, <class 'str'>, 13)

>>> [callable(obj) for obj in (abs, str, 13)]

[True, True, False]

5.5 用户定义的可调用类型

不仅Python函数是真正的对象,任何Python对象都可以表现得像函数。为此,只需要实现实例方法__call__.

>>> class BingoCage:

...     def __init__(self, items):

...         self._items = list(items)

...         random.shuffle(self._items) #将列表的所有元素随机排序

...     def pick(self):

...         try:

...             return self._items.pop()

...         except IndexError:

...             raise LookupError('pick from empty BingoCage')

...     def __call__(self):

...         return self.pick()

... 

>>> bingo = BingoCage(range(3))

>>> bingo.pick()

0

>>> bingo()

1

>>> callable(bingo)

True

5.6 函数内省

对象自省

自省(introspection),在计算机编程领域里,是指在运行时来判断一个对象的类型的能力。它是Python的强项之一。Python中所有一切都是一个对象,而且我们可以仔细勘察那些对象。Python还包含了许多内置函数和模块来帮助我们。

除了__doc__,函数对象还有很多属性。

>>> dir(factorial)

['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

示例5-9 列出常规对象没有而函数有的属性

>>> class C:pass

... 

>>> obj = C()

>>> def func():pass

... 

>>> sorted(set(dir(func))-set(dir(obj)))

['__annotations__', '__call__', '__closure__', '__code__', '__defaults__', '__get__', '__globals__', '__kwdefaults__', '__name__', '__qualname__']

5.7 从定位参数到仅限关键字参数

Python最好的特性之一是提供了极为灵活的参数处理机制。

>>> def tag(name, *content, cls=None, **attrs):

...     """生成一个多个HTML标签"""

...     if cls is not None:

...         attrs['class'] = cls

...     if attrs:

...         attr_str = ''.join('%s = "%s"' %(attr, value) for attr, value in sorted(attrs.items()))

...     else:

...         attr_str = ''

...     if content:

...         return '\n'.join('<%s%s>%s</%s>'%(name, attr_str, c, name)for c in content)

...     else:

...         return '<%s%s />' % (name, attr_str)

... 

>>> tag('br')

'<br />'

>>> tag('p', 'hello')

'<p>hello</p>'

>>> print(tag('p', 'hello', 'world'))

<p>hello</p>

<p>world</p>

>>> tag('p', 'hello', id=33)

'<pid = "33">hello</p>'

>>> print(tag('p', 'hello', 'world', cls='sidebar'))

<pclass = "sidebar">hello</p>

<pclass = "sidebar">world</p>

>>> tag(content='testing', name='img')

'<imgcontent = "testing" />'

>>> my_tag = {'name': 'img', 'title': 'Sunset Boulevard', 'src': 'sunset.jpg', 'cls':'framed'}

>>> tag(**my_tag)

'<imgclass = "framed"src = "sunset.jpg"title = "Sunset Boulevard" />'

5.8 获取关于参数的信息

函数对象有个__defaults__属性,它的值是一个元组,里面保存着定位参数和关键字参数的默认值。仅限关键字参数的默认值在__kwdefaults__属性中。然而,参数的名称在__code__属性中,它的值是一个code对象引用。

示例5-15 在指定长度附近截断字符串的函数

>>> def clip(text, max_len=80):

...     """在max_len前面或后面的第一个空格处截断文本"""

...     end = None

...     if len(text) > max_len:

...         space_before = text.rfind(' ', 0, max_len)

...         if space_before >= 0:

...             end = space_before

...         else:

...             space_after = text.rfind(' ', max_len)

...         if space_after >= 0:

...             end = space_after

...     if end is None:

...         end = len(text)

...     return text[:end].rstrip()

>>> clip.__defaults__

(80,)

>>> clip.__code__

<code object clip at 0x7fba902edd20, file "<stdin>", line 1>

>>> clip.__code__.co_varnames

('text', 'max_len', 'end', 'space_before', 'space_after')

>>> clip.__code__.co_argcount

2

可以看出,这种组织信息的方式并不是最便利的。参数名称在__code__.co_varnames中,不过里面还有函数定义体中创建的局部变量。因此,参数名称是前N个字符串,N的值由__code__.co_argcount确定。

幸好,由更好的方式————使用inspect模块

示例5-17 提取函数的签名

>>> from inspect import signature

>>> sig = signature(clip)

>>> sig

<Signature (text, max_len=80)>

>>> for name, param in sig.parameters.items():

...     print(param.kind, ':', name, '=', param.default)

... 

POSITIONAL_OR_KEYWORD : text = <class 'inspect._empty'>

POSITIONAL_OR_KEYWORD : max_len = 80

inspect.Signature对象有个bind方法,它可以把任意个参数绑定到签名中的形参上,所用的规则与实参到形参的匹配方式一样。框架可以使用这个方法在真正调用函数前验证参数。

示例5-18 把tag函数的签名绑定到一个参数字典上

>>> import inspect

>>> sig = inspect.signature(tag)

>>> my_tag={'name':'img', 'title':'Sunset Boulevard', 'src':'sunset.jpg', 'cls':'framed'}

>>> bound_args = sig.bind(**my_tag)

>>> bound_args

<BoundArguments (name='img', cls='framed', attrs={'title': 'Sunset Boulevard', 'src': 'sunset.jpg'})>

>>> del my_tag['name']

>>> bound_args = sig.bind(**my_tag)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 2997, in bind

    return args[0]._bind(args[1:], kwargs)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 2912, in _bind

    raise TypeError(msg) from None

TypeError: missing a required argument: 'name'

>>>

5.9 函数注解

Python3提供了一种句法,用于为函数声明中的参数和返回值附加元数据。

示例5-19 有注解的clip函数

>>> def clip(text:str, max_len:'int > 0'=80) -> str:

...     """在max_len前面或后面的第一个空格处截断文本"""

...     end = None

...     if len(text) > max_len:

...         space_before = text.rfind(' ', 0, max_len)

...         if space_before >= 0:

...             end = space_before

...         else:

...             space_after = text.rfind(' ', max_len)

...             if space_after >= 0:

...                 end = space_after

...     if end is None:

...         end = len(text)

...     return text[:end].rstrip()

注解不会做任何处理,只是存储在函数的__annotations__属性中:

>>> clip.__annotations__

{'text': <class 'str'>, 'max_len': 'int > 0', 'return': <class 'str'>}

Python对注解不做检查、不做强制、不做验证,什么操作都不做。换句话说,注解对Python解释器没有任何意义。注解只是元数据,可以供IDE、框架和装饰器等工具使用。

5.10 支持函数式编程的包

5.10.1 operator 模块

在函数式编程中,经常需要把算术运算符当作函数使用。

operator模块为多个算术运算符提供了对应的函数,从而避免编写lambda a, b: a * b这种匿名函数

>>> from functools import reduce

>>> from operator import mul

>>> def fact(n):

...     return reduce(mul, range(1, n+1))

5.10.2 使用functools.partial冻结参数

functools模块提供了一系列高阶函数,其中最为人熟知的或许是reduce,余下的函数中最有用的是partial及其变体,partialmethod。

functools.partial这个高阶函数用于部分应用一个函数。部分应用是指,基于一个函数创建一个新的可调用对象,把原函数的某些参数固定。使用这个函数可以把接受一个或多个参数的函数改编成需要回调的API,这样参数更少。

>>> from operator import mul

>>> from functools import partial

>>> triple = partial(mul, 3)

>>> triple(7)

21

>>> list(map(triple, range(1, 10)))

[3, 6, 9, 12, 15, 18, 21, 24, 27]