阅读笔记4:一等函数

1.概述
 
在Python中,函数是一等对象
 
一等对象的定义:
在运行时创建
能赋值给变量或数据结构中的元素
能作为参数传给函数
能作为函数的返回结果
 
在python中,整数,字符串和字典都是一等对象
 
2. 把函数视作对象
 
python函数是对象,是function类的实例
 
函数可以赋值给变量,也可以作为参数传给其他函数
 
>>> def doublefunc(value):
    return value * 2

>>> myfuc = doublefunc
>>> myfuc
<function doublefunc at 0x043702B8>
>>> myfuc(5)
10
>>> map(doublefunc, range(10))
<map object at 0x0436EF30>
>>> list(map(doublefunc, range(10)))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

3.高阶函数
 
高阶函数:
接受函数作为参数,或者把函数作为结果返回的函数是高阶函数, 比如 map sorted
 
map filter 和reduce的替代品:
列表推导替代map和filter

>>> list(map(doublefunc, range(10)))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> [doublefunc(n) for n in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> list(map(doublefunc, filter(lambda n: n%2, range(10))))
[2, 6, 10, 14, 18]
>>> [doublefunc(n) for n in range(10) if n % 2]
[2, 6, 10, 14, 18]

python2中,reduce是内置函数,在python 3放到functools模块了。可用sum取代reduce
>>> from functools import reduce
>>> from operator import add
>>> reduce(add, range(100))
4950
>>> sum(range(100))
4950
 
all和any也是内置的归约函数
 
all(iterable)
如果iterable的每个元素都是真值,返回True; all([])返回True
 
any(iterable)
只要iterable中有元素是真值,返回True; any([])返回False

 

4.匿名函数

python用lambda创建匿名函数
但是lambda函数的定义体只能使用纯表达式;也就是说不能赋值,也不能使用while和try等python语句
>>> fruits = ['strawberry', 'apple', 'cherry', 'banana']
>>> sorted(fruits, key = lambda word: word[::-1])
['banana', 'apple', 'strawberry', 'cherry']

除了作为参数传给高阶函数之外,python很少使用匿名函数

 

5.可调用对象

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

python中的可调用对象:
1)用户定义的函数
     使用def或lambda表达式创建
2)内置函数
     使用c语言(cPython)实现的函数, 如len 或者time.strftime
3)内置方法
     使用c语言实现的方法,如dict.get
4)方法
     在类的定义体中定义的函数
5)类
     调用类时会运行类的__new__方法创建一个实例,然后运行__init__方法,初始化实例,最后把实  
     例返回给调用方。因为python没有new运算符,所以调用类就相当于调用函数
6)类的实例
     如果类定义了__call__方法,那么它的实例可以作为函数调用
7)生成器函数
     使用yield关键字的函数或方法。调用生成器函数返回的是生成器对象

 

6.用户定义的可调用类型

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

import random
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()
1 >>> bingo()
0 >>> callable(bingo)
True

 

7.函数内省     

列出常规对象没有而函数有的属性:

>>> class C:pass

>>> obj = c()
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    obj = c()
NameError: name 'c' is not defined
>>> obj = C()
>>> def func(): pass

>>> sorted(set(dir(func))-set(dir(obj)))
['__annotations__', '__call__', '__closure__', '__code__', '__defaults__', '__get__', '__globals__', '__kwdefaults__', '__name__', '__qualname__']
>>>

用户定义的函数的属性:

名称 类型 说明
__annotations__ dict 参数和返回值的注解
__call__ methodwrapper 实现 () 运算符;即可调用对象协议
__closure__ tuple 函数闭包,即自由变量的绑定(通常是 None)
__code__ code 编译成字节码的函数元数据和函数定义体
__defaults__ tuple 形式参数的默认值
__get__ methodwrapper 实现只读描述符协议
__globals__ dict 函数所在模块中的全局变量
__kwdefaults__ dict 仅限关键字形式参数的默认值
__name__ str 函数名称
__qualname__ str 函数的限定名称,如 Random.choice( 参阅PEP
3155,https://www.python.org/dev/peps/pep-3155/)


8.从定位参数到仅限关键字参数

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函数的调用:
>>> 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)
'<p id="33">hello</p>'
>>> print(tag('p', 'hello', 'world', cls='sidebar'))
<p class="sidebar">hello</p>
<p class="sidebar">world</p>
>>> tag(content='testing', name="img")
'<img content="testing" />'
>>> my_tag = {'name': 'img', 'title': 'Sunset Boulevard',
... 'src': 'sunset.jpg', 'cls': 'framed'}
>>> tag(**my_tag)
'<img class="framed" src="sunset.jpg" title="Sunset Boulevard" />'


cls只能作为关键字参数传入

仅限关键字参数是python3新增的特性。
定义函数若想指定仅限关键字参数,要把它们放到前面有*的参数后面
如果不想支持数量不定的定位参数,想支持仅限关键字参数,那么在签名中放一个*

>>> def f(a, *, b):
... return a, b
...
>>> f(1, b=2)
(1, 2)


9.获取关于参数的信息

假设定义了如下函数:

>>> def clip(text, max_len=80):
    """get text"""
    end = None
    if len(text) > max_len:
        space_before = text.rfind('', 0, max_len)
        if spece_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 000000000110FCB0, file "<pyshell#80>", line 2>
>>> clip.__code__.co_varnames
('text', 'max_len', 'end', 'space_before', 'space_after')
>>> clip.__code__.co_argcount
2

在python 3 中,使用inspect模块:

>>> from clip import clip
>>> from inspect import signature
>>> sig = signature(clip)
>>> sig # doctest: +ELLIPSIS
<inspect.Signature object at 0x...>
>>> str(sig)
'(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

10.函数注解

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

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()


函数声明中的各个参数可以在:之后增加注解表达式
如果参数有默认值,注解放在参数名和=号之间。
如果想注解返回值,在)和函数声明末尾的:之间添加 ->和一个表达式,表达式可以是任何类型,注解中最常用的类型是类(str,int)和字符串('int > 0')

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

>>> from clip_annot import clip
>>> clip.__annotations__
{'text': <class 'str'>, 'max_len': 'int > 0', 'return': <class 'str'>}

可以用inspect,signature()提取注释

>>> from clip_annot import clip
>>> from inspect import signature
>>> sig = signature(clip)
>>> sig.return_annotation
<class 'str'>
>>> for param in sig.parameters.values():
... note = repr(param.annotation).ljust(13)
... print(note, ':', param.name, '=', param.default)
<class 'str'> : text = <class 'inspect._empty'>
'int > 0' : max_len = 80

11.支持函数式编程的包

11.1 operator模块

operator.mul


from functools import reduce
from operator import mul
def fact(n):
return reduce(mul, range(1, n+1))

operator.itemgetter


根据元组的某个字段给元组列表排序
>>> metro_data = [
... ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
... ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
... ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
... ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
... ('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
... ]
>>>
>>> from operator import itemgetter
>>> for city in sorted(metro_data, key=itemgetter(1)):
... print(city)
...
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889))
('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
('Mexico City', 'MX', 20.142, (19.433333, -99.133333))
('New York-Newark', 'US', 20.104, (40.808611, -74.020386))


如果把多个参数传递给itemgetter,它构建的函数会返回提取的值构成的元组:
>>> cc_name = itemgetter(1, 0)
>>> for city in metro_data:
... print(cc_name(city))
...
('JP', 'Tokyo')
('IN', 'Delhi NCR')
('MX', 'Mexico City')
('US', 'New York-Newark')
('BR', 'Sao Paulo')
>>>


itemgetter不仅支持序列,还支持映射和任何实现__getitem__方法的类

operator.attrgetter


它创建的函数根据名称提取对象的属性。如果把多个属性传给attrgetter,它会返回提取的值构成的元组。如果参数中包含.,它会深入嵌套对象,获取指定的属性
>>> from collections import namedtuple
>>> LatLong = namedtuple('LatLong', 'lat long') #
>>> Metropolis = namedtuple('Metropolis', 'name cc pop coord') #
>>> metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) #
... for name, cc, pop, (lat, long) in metro_data]
>>> metro_areas[0]
Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722,
long=139.691667))
>>> metro_areas[0].coord.lat #
35.689722
>>> from operator import attrgetter
>>> name_lat = attrgetter('name', 'coord.lat') #
>>>
>>> for city in sorted(metro_areas, key=attrgetter('coord.lat')): #
... print(name_lat(city)) #
...
('Sao Paulo', -23.547778)
('Mexico City', 19.433333)
('Delhi NCR', 28.613889)
('Tokyo', 35.689722)
('New York-Newark', 40.808611)

operator.methodcaller


它创建的函数会在对象上调用参数指定的方法
>>> from operator import methodcaller
>>> s = 'The time has come'
>>> upcase = methodcaller('upper')
>>> upcase(s)
'THE TIME HAS COME'
>>> hiphenate = methodcaller('replace', ' ', '-')
>>> hiphenate(s)
'The-time-has-come'

11.2 使用functools.partial冻结参数

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]

posted on 2018-09-26 14:02  cherryjing0629  阅读(148)  评论(0编辑  收藏  举报

导航