is there a 'foreach' function in python3?
when i meet the situation i can do it in javascript, i always think if
there's an 'foreach' function it would be convenience .a foreach function
can be described as this below:
def foreach(fn,iterable):
    for x in iterable:
        fn(x)
they just do it on every element and didn't yield or return something,i
think it sould be a built-in function and should be more faster than
writing it with pure python,but i didn't found it on the list,or it just
called another name?or i just miss some points here?
UPDATE:maybe i got wrong,cause calling an function in python cost
high,definitely not a good practice for the example.rather than an out
loop,the function should do the loop in side its body looks like this
below which already mentioned in many python's code suggestions:
def fn(*args):
    for x in args:
       dosomething
but i thought foreach is still welcome base on the two facts:
1.in normal cases,people just don't care about the performance 2.sometime
the api didn't accept iterable object and you can't rewrite its source.
 
No comments:
Post a Comment