Create a list of lambda:
L = [lambda x: x ** 2, # Inline function definition lambda x: x ** 3, lambda x: x ** 4] # A list of three callable functions # w w w . j a v a 2 s. co m for f in L: print(f(2)) # Prints 4, 8, 16 print(L[0](3)) # Prints 9
Which is the same as follows:
def f1(x): return x ** 2 def f2(x): return x ** 3 # Define named functions def f3(x): return x ** 4 L = [f1, f2, f3] # Reference by name # w ww. ja v a2s. co m for f in L: print(f(2)) # Prints 4, 8, 16 print(L[0](3)) # Prints 9