zoukankan      html  css  js  c++  java
  • python的partial()用法说明

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

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

    # 以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,也就是"冻结"了这个参数。

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

    之所以"冻结"加上了引号,是因为可以在inttwo()中再次指定参数来覆盖partial()中"冻结"的参数:

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

    回头再看partial()的定义:

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

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

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

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

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

    >>> 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()也很少用。

  • 相关阅读:
    python面试大全
    python面试2
    python求职之路
    python面试题目
    关于栈的输入顺序和输出顺序
    表单提交中get和post方式的区别
    DOS命令行下mysql 基本命令
    跨站请求伪造CSRF
    Windows上python的virtualenv 安装及使用
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/f-ck-need-u/p/10198013.html
Copyright © 2011-2022 走看看