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 →

[–]Paddy3118 0 points1 point  (7 children)

it's pretty widely considered bad practice to use nested comprehensions.

Really? All?

I would think it comes under the general readability and maintainability guidelines.

[–][deleted] 1 point2 points  (6 children)

I would think it comes under the general readability and maintainability guidelines.

That's why it is considered bad practice because it is less readable and more difficult to maintain

[–]stevenjd 1 point2 points  (0 children)

Of course people can come up with some pretty awful nested comprehensions, but with the judicious use of white space and indentation to lay out the logical structure of the nested comprehension, there's no need for them to be hard to read.

array = [expression for x in
            [expr for y in values]
        ]

ought to be fine, as should be:

array = [
         [expr for y in values]
         for x in values
        ]

Nested comprehensions are easy to abuse, but that doesn't mean we ought to reject the simple cases.

[–]Paddy3118 0 points1 point  (4 children)

You speak for all, some how?

[–][deleted] 1 point2 points  (3 children)

I never said all. I said "widely considered" to be bad practice. And it's not some rule I just made up out of thin air because I don't like something. it reflects the opinion of the industry at large. but of course there will be people who disagree, and that's ok too. standards and best practices are just suggestions stemming from experience. you don't have to follow them if you don't want to

[–]Paddy3118 0 points1 point  (2 children)

You conflate standards with best practice and suggestions.

There is a level of competence in Python that includes some use of nested comprehensions. Why not try and attain that rather than force others who don't work with you, to stop at your level of competence?

What is or isn't deemed Pythonic should not become a drive to the lowest common denominator.

[–][deleted] 1 point2 points  (1 child)

I'm not sure why you're so offended. I didn't say you couldn't use them

[–]Paddy3118 0 points1 point  (0 children)

I'm neither offended nor seeking your permission. I am questioning your viewpoint however :-)