2

If I have a function with some arguments, I can define a duck function like this:

>>> def f(x, y=0, z=42): return x + y * z 
... 
>>> f(1,2,3)
7
>>> g = f
>>> f(1,2)
85
>>> g(1,2)
85

I've tried to override the arguments partially but this didn't work:

>>> g = f(z=23)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes at least 1 argument (1 given)

How do I define function arguments partially for the duck function?

0

1 Answer 1

3

Use functools.partial

>>> from functools import partial
>>> def f(x, y=0, z=42): return x + y * z
... 
>>> g = partial(f, z=23)
>>> g(1,2)
47
>>> f(1,2,23)
47
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.