-2

While I am using my partial function I am unable to do the following code

from functools import partial

def f(a, b, c, d):
    return 100 * a + 50 *b + 25 * c + d

g = partial(f, b=2)

print g(2, 4, 5)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    Can you describe the error that occurs, and what you expect these function to do, including a [minimal, verifiable, complete example](https://stackoverflow.com/help/mcve)? – shayaan Feb 02 '19 at 09:15
  • You're passing both `2` and `4` for `b`, try `g(2, c=4, d=5)`. – jonrsharpe Feb 02 '19 at 09:21
  • Also this may be useful: https://stackoverflow.com/questions/15331726/how-does-the-functools-partial-work-in-python – Denis Rasulev Feb 02 '19 at 09:22
  • 1
    "I am unable to do the following code" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Feb 02 '19 at 09:39
  • This should have been more descriptive to your challenge:TypeError Traceback (most recent call last) in 6 g = partial(f, b=2) 7 ----> 8 print(g(2, 4, 5)) TypeError: f() got multiple values for argument 'b' – MUNGAI NJOROGE Feb 02 '19 at 10:45

2 Answers2

1

You can't make partial with positional arguments other than last.

If you need to make partial of b, you need to pass c and d by name:

g = partial(f, b=2)
print(g(2, c=4, d=5))

Or use lambda (or wrapper function) if you need to pass positional args

Slam
  • 8,112
  • 1
  • 36
  • 44
0

we can call g() to check the function, then you get a info:

TypeError: f() missing 3 required positional arguments: 'a', 'c', and 'd'

Now, we can know that there is an argument b,we can specify the arguments c and d:

g(2, c=4, d=5)
405
RayZen
  • 141
  • 1
  • 4