0

I have defined a two-variables function like

def f(x1, x2):
    return x1 + x2

Now, I want to pass a series of x2 into function f without calling it, to create a series of new functions. How can I achieve that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    What do you mean by `pass a series of x2` – Yedidya Rashi Jun 11 '22 at 12:11
  • 3
    What do you mean ‘pass into a function without calling it’? Sounds a bit like ‘how do I go swimming, without getting into the water’. Please clarify. – S3DEV Jun 11 '22 at 12:14
  • 3
    Does this answer your question? [How does functools partial do what it does?](https://stackoverflow.com/questions/15331726/how-does-functools-partial-do-what-it-does) – buran Jun 11 '22 at 12:30
  • 1
    Why closed? The phrase: `"create a series of new functions"` seems clear to me. There is even an answer here on that basis. – quamrana Jun 11 '22 at 12:35
  • @quamrana existing close reason isn't good, but it's a dupe of [Creating Python function with partial parameters](https://stackoverflow.com/q/3258756) – SuperStormer Jun 13 '22 at 04:10
  • @SuperStormer: Ok, I'll make a note of that. – quamrana Jun 13 '22 at 06:48

1 Answers1

2

If I understand your question correctly, this sounds like a job for functools.partial:

def f(x1, x2):
    return x1 + x2

from functools import partial
funcs = [partial(f, x2=i) for i in range(10)]  # example use case
Dominik Stańczak
  • 2,046
  • 15
  • 27