[Python-ideas] A real life example of "given"

Michael Selik mike at selik.org
Fri Jun 8 19:06:46 EDT 2018


What's wrong with making this two lines?


    In [1]: import random

    In [2]: xs = [10, 20, 30]

    In [3]: def foo(x):
        ...:     return [x + i for i in range(3)]
        ...:
        ...:

    In [4]: def bar(y):
        ...:     if random.random() < 0.3:
        ...:         return None
        ...:     return str(y)
        ...:
        ...:

    In [5]: ys = ((y, bar(y)) for x in xs for y in foo(x))

    In [6]: {y: result for y, result in ys if result is not None}
    Out[6]: {10: '10', 11: '11', 20: '20', 21: '21', 22: '22', 30: '30', 
32: '32'}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180608/e12c1a50/attachment.html>


More information about the Python-ideas mailing list