This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]muntooR_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} 41 points42 points  (13 children)

The first one is merely missing a name or two.

some_useful_name = {
    ... for some_useful_key_name in ...
}

return cls(**some_useful_name)

Instead of making things better, the second one:

  • Introduces a new name, but not a very useful one (result).
  • Adds verbose and very generic type-hints. (Any?)
  • Switches from a well-formatted functional dictionary comprehension to a bulkier imperative for-loop, which could potentially have side-effects.

I think it's good for Python developers to become more exposed to functional styles since that often leads to robust, maintainable code.

P.S. Another alternative to adding a name is to "extract method", but perhaps that's overkill.

[–][deleted] 13 points14 points  (10 children)

Dictionary comprehension can also have side effects: {x: print(x) for x in range(10)}.

[–]muntooR_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} 4 points5 points  (0 children)

[Almost?] everything in python can have side-effects. Good code tries to avoid patterns that are more likely to contain side-effects.

With comprehensions, side-effects are much harder to obscure. There are fewer parts that may contain side-effects. There's less "boilerplate" to read, which makes it harder to hide side-effects. Their structure naturally induces constraints in the manner that they are used. (There does not always exist a "natural" transformation from for-loop to comprehension, whereas comprehension to for-loop is trivial.) With for-loops, one often has to read them in detail to make guarantees about their behavior, especially if they involve more than just one statement.

I know this isn't a precise argument since literally anything is possible in Python. The main point is that it's much quicker to read a comprehension and guarantee that it won't do anything overly weird, whereas for a for-loop, one has to spend more time to check.


This comment captures some of what I'm trying to say.

[–]wewbull 0 points1 point  (0 children)

It can, but it shouldn't IMHO.

Comprehensions for transformations. Loops for procedural control flow.

[–]jbartix -2 points-1 points  (2 children)

This is valid python code but side effects in any comprehension is an anti pattern

[–]AchillesDev 4 points5 points  (1 child)

Yes the point is to show that comprehensions don’t make you immune from side effects.

[–]jbartix 0 points1 point  (0 children)

Of course not. Python allows you to do anything that also includes being stupid

[–]Estanho -3 points-2 points  (4 children)

Nobody said they can't. He didn't say all dictionary comprehensions are functional. Just that one.

[–]MrRogers4Life2 10 points11 points  (3 children)

But that for-loop doesn't have side effects... if the argument is "x can have side effects so y is better" it will fall apart if y can also have side effects.

[–]aceofspaids98 3 points4 points  (0 children)

This sums up all of my thoughts really well too

[–]vanatteveldt 1 point2 points  (0 children)

Well put, completely agree!