Spaceship222
Spaceship222

Reputation: 849

Different behavior on `functools.partial`

Why get_score would cause such Error but fx don't

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

Upvotes: 0

Views: 48

Answers (1)

John Zwinck
John Zwinck

Reputation: 249143

getitem() is probably implemented in C and not Python, and does not support keyword arguments. Implementation of Python functions using the C API is considerably different to implementation using Python itself. In particular the argument parsing is more explicit when using the C API.

Upvotes: 1

Related Questions