简体   繁体   English

在`functools.partial`上的不同行为

[英]Different behavior on `functools.partial`

Why get_score would cause such Error but fx don't 为什么get_score会导致此类Errorfx不会

from operator import getitem
from functools import partial
# getitem(a, b) -- Same as a[b]

d = dict(name='foo', score=100)
get_score = partial(getitem, b='score')
get_score(d)
# expect 100 but 
# TypeError: getitem() takes no keyword arguments

def f(x, y):
    return x+y
fx = partial(f, y=2)
fx(5) == 7 # True

getitem() is probably implemented in C and not Python, and does not support keyword arguments. getitem()可能是用C而不是Python实现的,并且不支持关键字参数。 Implementation of Python functions using the C API is considerably different to implementation using Python itself. 使用C API实现Python函数与使用Python本身实现有很大不同。 In particular the argument parsing is more explicit when using the C API. 特别是在使用C API时,参数解析更加明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM