[Python-ideas] Fwd: New suggested built in keyword: do

Steven D'Aprano steve at pearwood.info
Fri Jun 8 12:01:31 EDT 2018


On Fri, Jun 08, 2018 at 10:12:01AM -0400, Randy Diaz wrote:

> I think that the keyword do would solve problems that occur when people
> want a simple way to run a command over an iterable but they dont want to
> store the data.

Why does it have to be a keyword?

I like this pair of functions:

def do(func, iterable, **kwargs):
    for x in iterable:
        func(x, **kwargs)

def star(func, iterable, **kwargs):
    for x in iterable:
        func(*x, **kwargs)

do.star = star
del star


Here's an example in use:


py> do(print, [(1, 2), (3, 4), (5, 6, 7, 8)], sep='-', end='*\n')
(1, 2)*
(3, 4)*
(5, 6, 7, 8)*
py> do.star(print, [(1, 2), (3, 4), (5, 6, 7, 8)], sep='-', end='*\n')
1-2*
3-4*
5-6-7-8*

Customize to your taste, and put them in your own personal toolbox.



-- 
Steve


More information about the Python-ideas mailing list