|
|
Subscribe / Log in / New account

Corner cases and exception types

Corner cases and exception types

Posted Aug 18, 2019 12:12 UTC (Sun) by robert_s (subscriber, #42402)
In reply to: Corner cases and exception types by Cyberax
Parent article: Corner cases and exception types

While I can see its use in `if` statements, its use in comprehensions seems to encourage an extremely confusing approach, mutating state in the middle of comprehensions. The power and clarity of comprehension statements comes from their use as functional, pure constructs. All of the examples I've seen of the walrus operator used in comprehensions are confusing at best.


to post comments

Corner cases and exception types

Posted Aug 21, 2019 20:27 UTC (Wed) by smurf (subscriber, #17840) [Link] (2 responses)

So how would you rewrite the "filtered_data" example? It's plenty concise for me.

Corner cases and exception types

Posted Aug 22, 2019 16:34 UTC (Thu) by nybble41 (subscriber, #55106) [Link]

filtered_data = [y for y in map(f, data) if y is not None]

Corner cases and exception types

Posted Aug 25, 2019 22:43 UTC (Sun) by nevyn (guest, #33129) [Link]

So how would you rewrite the "filtered_data" example? It's plenty concise for me.
Which is:
    filtered_data = [y for x in data if (y := f(x)) is not None]
...the obvious simple choice is:
    # Just iterate data and return the value we want...
    filtered_data = [x for x in iter_f(data) if x is not None]
...which leads to the even better version:
    # Get a list of filtered values we want...
    filtered_data = list_filter_f(data)
...the original idea behind python wasn't to cram everything on a single line to win perl golf competitions.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds