What does it mean for a function to take a list of instances of a method in Python?
How would you implement that?
Asked
Active
Viewed 126 times
0

Phantômaxx
- 37,901
- 21
- 84
- 115

Epoch79
- 9
- 1
-
1This is a specification of input, you *cannot* implement the specs of input, these are given to you. It is up to the caller to respect these contracts, not the callee. – Willem Van Onsem Jan 22 '17 at 21:54
-
Can you be more specific? What is asking you this? – Soviut Jan 22 '17 at 22:01
3 Answers
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