When I encounter a situation, I can do it in javascript, I always think that if the foreach function is convenient. In foreach, I mean the function that is described below:
def foreach(fn,iterable): for x in iterable: fn(x)
they just do it on every element and do not give or return anything, I think it should be a built-in function and should be faster than writing it with pure Python, but I did not find it on the list, or it is simply called by a different name Or am I just missing a few points here?
Maybe I'm wrong because calling a function in Python is expensive, definitely not a good practice for an example. Instead of exiting the loop, the function should make a loop inside, its body looks like the one shown below, which has already been mentioned in many sentences of python code:
def fn(*args): for x in args: dosomething
but I thought that foreach is still welcomed on the basis of two facts:
- In ordinary cases, people just don't care about performance
- Once upon a time, the API did not accept an iterative object, and you cannot rewrite its source.
user2003548 Aug 18 '13 at 0:14 2013-08-18 00:14
source share