0

I want to store multiple functions inside an array and execute one function when i call array[index] to be executed and not all the functions inside the array, how to do it in python?

Example:

     array = [  function2(1, 2), function3(2, 3)]
     array[0] #Execute array[0] and not all the functions inside array
Boutros
  • 207
  • 1
  • 3
  • 15
  • 1
    To store functions, you'd do `array = [ function2, function3 ]`. To call a function you need `()`: `array[0](1, 2)`. – melpomene Nov 01 '18 at 23:11
  • @melpomene Is their a way to pass parameters inside the array and not when doing array[0]? – Boutros Nov 01 '18 at 23:14
  • @aranfey I don't see how that's a duplicate. – melpomene Nov 01 '18 at 23:15
  • @melpomene The OP wants to store partially (or completely) applied functions a list and call them later, no? Feel free to reopen if you disagree though. – Aran-Fey Nov 01 '18 at 23:25
  • @Aran-Fey OP wants accessing an array element (`array[0]`) to call a function instead (`function2(1, 2)`). The whole thing looks like an XY problem to me. – melpomene Nov 01 '18 at 23:27
  • Well, if the OP really can't stand the minor change in syntax (`array[0]()`), then all they need to do is write a `list` subclass that does that automatically. But they can't expect us to write that class for them, so I think the dupe is fine. – Aran-Fey Nov 01 '18 at 23:30

0 Answers0