Re:WTFPython: Exploring and understanding Python through sur...
by quietbritishjim on Tuesday May 31, 2022 @08:34AM
(#31568781)
I suspect the question was rhetorical. The point is, every reader is going to have that question pop into their head and have to look it up. Better to use code that doesn't raise any questions, even if it's a few more characters.
[ Parent
| on HN ]
Re:WTFPython: Exploring and understanding Python through sur...
by dragonwriter on Tuesday May 31, 2022 @01:21PM
(#31572193)
> Better to use code that doesn't raise any questions, even if it's a few more characters.
Certainly, I agree; I would usually use:
(x for x in xs if x)
Or, if I know more about the kind of falsy values xs actually needs removed, something more explicit like: (x for x in xs if x is not None)
Because Python’s multiplicity of falsy values can also be something of a footgun (particularly, when dealing with something a collection of Optionals where the substantive type has a falsy value like 0 or [] included.)Instead of:
filter(None, xs)
Which is terse but potentially opaque.Though it's additional syntax, I kind of wish genexp/list/set comprehensions could use something like “x from” as shorthand for “x for x in”, which would be particularly nice for filtering comprehensions.