I have a list of callback functions that I need to invoke when an event is fired.
Is this idiomatic python?
def first_callback(m):
print 'first ' + m
def second_callback(m):
print 'second ' + m
lst = [first_callback, second_callback]
map(lambda x: x("event_info"),lst) #is this how you do it?
Use
maponly for functions without side effects (likeprint). That is, use it only for functions that just return something. In this case a regular loop is more idiomatic:Edit: also, as of Python 3.0,
mapreturns an iterator instead of a list. Hence in Python 3.0 the code given in the question will not call any function, unless all elements in the generator are evaluated explicitly (e.g. by encapsulating the call tomapinsidelist). Luckily the 2to3 tool will warn about this:File
map.py:2to3-3.0 map.pyoutput: