簡體   English   中英

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

[英]Different behavior on `functools.partial`

為什么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()可能是用C而不是Python實現的,並且不支持關鍵字參數。 使用C API實現Python函數與使用Python本身實現有很大不同。 特別是在使用C API時,參數解析更加明確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM