V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Dimen61
V2EX  ›  Python

python 中 functools.partial 的参数用法疑惑

  •  
  •   Dimen61 · 2016-08-11 20:18:59 +08:00 · 2777 次点击
    这是一个创建于 2786 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我已经知道的

    我们知道可以这样来用partial

    import functools
    int2 = functools.partial(int, base=2)
    print(int2('10001'))
    # Output: '17'
    

    int 函数的解释如下:

    class int(object)
     |  int(x=0) -> integer
     |  int(x, base=10) -> integer
     |
     |  Convert a number or string to an integer, or return 0 if no arguments
     |  are given.  If x is a number, return x.__int__().  For floating point
     |  numbers, this truncates towards zero.
    
    • 我对 partial 参数的理解是,加入这么写

      functools.partial(func, arg1)

    则指定 func 最靠左的那个参数为 arg1

    def fun(x, y, z):
        print('x: {0}; y: {1}; z: {2}'.format(x, y, z))
    
    f1 = functools.partial(fun, 1)
    f1(2, 3)
    # Output: 'x: 1; y: 2; z: 3'
    

    我们也可以通过在 parital 中通过指定默认参数来绑定 func 中的参数值

    f2 = functools.partial(fun, z=3)
    f2(2, 1)
    # Output: 'x: 2; y: 1; z: 3'
    

    我的问题

    sum_100 = functools.partial(sum, start=100)
    print(sum_100([1, 2, 3]))
    #TypeError: sum() takes no keyword arguments
    

    sum 函数的说明:

    sum(iterable, start=0, /)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers
    
    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.
    

    既然这样对 sum 函数不行,为什么能成功作用于 int 了

    import functools
    int2 = functools.partial(int, base=2)
    print(int2('10001'))
    # Output: '17'
    

    int 函数的解释如下:

    class int(object)
     |  int(x=0) -> integer
     |  int(x, base=10) -> integer
     |
     |  Convert a number or string to an integer, or return 0 if no arguments
     |  are given.  If x is a number, return x.__int__().  For floating point
     |  numbers, this truncates towards zero.
    
    6 条回复    2016-08-11 21:49:08 +08:00
    knightdf
        1
    knightdf  
       2016-08-11 20:28:27 +08:00
    sum(sequence[, start]) -> value
    Dimen61
        2
    Dimen61  
    OP
       2016-08-11 20:34:49 +08:00
    @knightdf 啥意思,可否详细解释下
    petelin
        3
    petelin  
       2016-08-11 20:43:07 +08:00 via Android
    sum 函数不接受 start 这个关键字参数,你找的函数解释跟你用的版本不对,或者别的什么的
    zhuangzhuang1988
        4
    zhuangzhuang1988  
       2016-08-11 20:51:31 +08:00
    python 源码参考这里
    https://github.com/python/cpython/blob/2.7/Python/bltinmodule.c#L2307

    sum 没有通过关键字获取 start
    ```python
    import functools
    def _sum(seq, start=0):
    return sum(seq, start)
    f = functools.partial(_sum, start=100)
    print f([1,2,3])
    ```
    这样解决。。
    eric6356
        5
    eric6356  
       2016-08-11 21:06:58 +08:00
    我猜你是用了 IPython 之类的看的说明。 IPython 会调用 inspect 去取对象的 signature 。而对于这种 built-in 函数,真正的参数并不像你看到的 signature 那样。还是应当以文档或者源码为准。
    Dimen61
        6
    Dimen61  
    OP
       2016-08-11 21:49:08 +08:00
    感谢大家,我是在 python3 终端交互下,用 help(sum)查看的函数解释
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5389 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:33 · PVG 15:33 · LAX 00:33 · JFK 03:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.