The code you specify is
for function in functions: function(x)
... does nothing with the result of calling function(x) . If this is true, that is, these functions are called for their side effects, then there is no longer an alternative to pythons. Just leave your code as it is. & dagger; Point to go home here in particular
Avoid features with side effects in the list settings.
As for efficiency: I expect that using something else instead of your simple loop will not improve the runtime. If in doubt, use timeit . For example, the following tests seem to indicate that a regular for-loop is faster than a list comprehension. (I would not want to draw any general conclusions from this test, I thought):
>>> timeit.Timer('[f(20) for f in functions]', 'functions = [lambda n: i * n for i in range(100)]').repeat() [44.727972984313965, 44.752119779586792, 44.577917814254761] >>> timeit.Timer('for f in functions: f(20)', 'functions = [lambda n: i * n for i in range(100)]').repeat() [40.320928812026978, 40.491761207580566, 40.303879022598267]
But then again, even if these tests would indicate that rethinking lists is faster, it remains that you should not use them when side effects are involved, for readability.
& dagger; : Well, I would write for f in functions , so the difference between function and functions more pronounced. But this is not said.
source share