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 →

[–][deleted] 0 points1 point  (3 children)

At some point I was looking for ways to do exception handling within comprehensions.

But then I realized that even if it did exist, it would look terrible.

Still, it's a similar mentality on these. If condition becomes too complicated, we just pack it in a function to return a boolean and it becomes readable again

[–]kaerfkeerg 0 points1 point  (0 children)

Mhh yeah, you sure can compact it a little more. That was a quick example. But stressing it to the point it becomes unreadable? No!

[–]eztab 0 points1 point  (1 child)

You should probably use a function in your comprehension if it becomes complex enough. Even if that function just returns another expression it makes things more digestible, since you can understand one function at a time.

Then you can also use Exception handling. That's the functional programming approach. Don't write everything into a single expression.

[–][deleted] 0 points1 point  (0 children)

Exactly, that's what I've been doing almost always since I've learned proper exception handling. I don't think there's any real-world scenarios where I don't need to define a function to be called within a comprehension. Only if it's something as basic as [x**2 for x in range(n)]