0

What does it mean for a function to take a list of instances of a method in Python?
How would you implement that?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Epoch79
  • 9
  • 1

3 Answers3

2

A function in Python is an object. You can store it in a variable or pass it as a parameter to a function. The whole concept of decorators is based on that.

# Accepts two instances of a method as input arguments
# Executes the two function instances
def twofuncs(fa, fb):
    fa(5)
    fb(10)

def original_fa(x):
    print("Function a", x)

# Instance of the above function
fa = original_fa

# Executing the main function
twofuncs(fa, fa)
pmuntima
  • 640
  • 5
  • 13
1

If we interpret it literally... Any function is just an object. You can have therefore have instances of functions, and you can place them in a list.

A function...

def func():
    pass

Takes a list...

def func(lst):
    pass

of instances of a method...

def add(x, y):
    return x+y

func([add, add, add])
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

What it means is that you're passing a method reference into a function as an argument. This has been asked before on this site. You can see this post or perhaps this one for an example of how this is done.

Community
  • 1
  • 1
Amorpheuses
  • 1,403
  • 1
  • 9
  • 13