python的partial()用法说明

在functools模块中有一个工具partial(),能够用来"冻结"一个函数的参数,并返回"冻结"参数后的新函数。html

很简单的解释,也是官方手册给的示例。对于int()函数,它能够将给定的数值转换成十进制整数,转换时能够指定以几进制的方式解析给定的数。例如:python

# 以10进制解析123,并转换成10进制整数
>>> int("123")
123

# 以2进制解析10101,并转换成10进制整数
>>> int("10101", base=2)
21

# 以13进制解析"abc12c",并转换成10进制整数
>>> int("abc12c", base=13)
4053672

如今不想这样指定base=2参数来将二进制转换为10进制整数了,而是像普通函数同样,直接指定待转换的值便可。因而,定义另一个函数来封装int(),例如:函数

def inttwo(x):
    return int(x, base=2)

inttwo("10101")

functools中提供的partial()就是作相似事情的:工具

inttwo = partial(int, base=2)

它表示int()中指定参数base=2,也就是"冻结"了这个参数。code

>>> from functools import partial
>>> inttwo = partial(int,base=2)
>>> inttwo("10101")
21

之因此"冻结"加上了引号,是由于能够在inttwo()中再次指定参数来覆盖partial()中"冻结"的参数:htm

>>> inttwo("10101",base=10)
10101

回头再看partial()的定义:对象

functools.partial(func, *args, **keywords)

从它的定义不难知道,不单单是像int()中base这样的kw参数格式,位置参数args也同样能"冻结"。文档

partial()返回的实际上是一个partial对象,这个对象包含了3个特殊的属性:get

>>> dir(inttwo)
[...... 'args', 'func', 'keywords']
  • func表示该对象所封装的原始函数
  • args表示"冻结"的位置参数列表
  • keywords表示"冻结"的关键字参数
>>> inttwo.func
<class 'int'>
>>> inttwo.args
()
>>> inttwo.keywords
{'base': 2}

另外须要注意的是,partial()不会保留封装函数的元数据,好比注释文档、注解等。io

>>> def myfunc(x:int, y:int) -> int:
...     ''' sum x + y '''
...     return x + y


# 函数元数据信息
>>> myfunc.__doc__
' sum x + y '
>>> myfunc.__annotations__
{'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}

# partial()包装后的函数,没有函数元数据
>>> newfunc = functools.partial(myfunc,y=3)

因此若是须要这些元数据,必须本身手动将元数据赋值给partial对象:

>>> newfunc.__doc__ = myfunc.__doc__
>>> newfunc.__annotations__ = myfunc.__annotations__
>>> newfunc.__doc__
' sum x + y '
>>> newfunc.__annotations__
{'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}

最后,除了partial()能够将函数的参数"冻结",functools还提供了partialmethod()将方法的参数"冻结",但基本上用不上,就连partial()也不多用。

相关文章
相关标签/搜索