Is it possible to store a function with predefined arguments to be called by another function?
For example:
def function(num):
print num
trigger=fun
You're looking for functools.partial:
>>> import functools
>>> def foo(number):
... print number
...
>>> bar = functools.partial(foo, 1)
>>> bar()
1
Someone already mentioned functools.partial
which is preferred, but you can also use lambda functions without arguments:
trigger1 = lambda: function(1)
trigger2 = lambda: function(2)
Note: As someone mentioned, be careful about defining functions like this in loops or by referencing any value in the lambda body that might change.
You might end up in a situation like this:
a = []
for i in range(5):
a.append(lambda: i)
b = [func() for func in a]
# equals [4, 4, 4, 4, 4]
# to avoid this, do a.append(lambda i=i: i)