python 中 functools.partial 的参数用法疑惑 - 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

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

  •  
  •   Dimen61 Aug 11, 2016 3625 views
    This topic created in 3568 days ago, the information mentioned may be changed or developed.

    我已经知道的

    我们知道可以这样来用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 replies    2016-08-11 21:49:08 +08:00
    knightdf
        1
    knightdf  
       Aug 11, 2016
    sum(sequence[, start]) -> value
    Dimen61
        2
    Dimen61  
    OP
       Aug 11, 2016
    @knightdf 啥意思,可否详细解释下
    petelin
        3
    petelin  
       Aug 11, 2016 via Android
    sum 函数不接受 start 这个关键字参数,你找的函数解释跟你用的版本不对,或者别的什么的
    zhuangzhuang1988
        4
    zhuangzhuang1988  
       Aug 11, 2016
    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  
       Aug 11, 2016
    我猜你是用了 IPython 之类的看的说明。 IPython 会调用 inspect 去取对象的 signature 。而对于这种 built-in 函数,真正的参数并不像你看到的 signature 那样。还是应当以文档或者源码为准。
    Dimen61
        6
    Dimen61  
    OP
       Aug 11, 2016
    感谢大家,我是在 python3 终端交互下,用 help(sum)查看的函数解释
    About     Help     Advertise     Blog     API     FAQ     Solana     5541 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 02:52 PVG 10:52 LAX 19:52 JFK 22:52
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86