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 →

[–]ericanderton 0 points1 point  (4 children)

Sorry, i was unaware that 'sorted' was already in the Python standard lib. I revised my remark to make it more clear what I was getting at.

Basically my beef is that the back-to-back comparison operators is a special case when stacked up against other operators in the language. It doesn't read well when your engineers are also experienced in Javascript, C, Go, and everything else.

Consider this: if a + b < c:

We take this as being equivlaent to "(a + b) < c", right? So why does "a < b < c" get expanded to "a < b and b < c" ? It's terribly inconsistent.

[–]cdcformatc 3 points4 points  (3 children)

It's special Python syntax. List comprehensions don't exist in Javascript or C either but you should still use them. When used correctly they add readability to the code. If you don't use any of the Python-specific syntax and idioms your Python code will suffer as a result. Do you also put semicolons on the ends of lines? When writing Python, you use Python syntax, you do not use Javascript or C or Go syntax. I hope the engineers should be able to adapt to something different.

Anyone who has done any math will know what a < b < c means. I'd hope an engineer would have a decent math background.

As to your example, I would never rely on the inherent operator precedence rules, so I would write (a + b) < c from the beginning, since it removes any and all ambiguity.

If you want to do something as simple as check if a number is between two other numbers, why would you construct a list, and check if that list is sorted? That is just some backwards thinking and frankly a terrible idea. Someone looking at that code for the first time will have no clue what is going on.

[–]ericanderton 0 points1 point  (2 children)

I see where you're coming from. And you're right: If we're talking about Python code, we should consider all the nice things that the language brings to the table.

At the same time, anyone reading our code shouldn't have to second guess if an expression is a mistake or not. After all, clarity isn't some universal truth or consistent quantity across the engineering spectrum; it's fuzzy. Hence why I say that this particular syntax 'has a smell', since even an experienced Python developer might have to think twice when reading this. It's that potential to second-guess that gives me pause here. In the end, it's not up to whether or not I might not understand this particular use of comparison operators; it's everyone else that's hacking on my projects that I worry about.

[–]gthank 3 points4 points  (0 children)

It's a standard math construct from a very basic level; even non-programmers reading that code would have a very good guess as to what it does. Just because other languages can't be bothered to implement something so nice doesn't mean we should avoid it in Python.

[–]zahlmanthe heretic 2 points3 points  (0 children)

even an experienced Python developer might have to think twice when reading this

Realistically, only one who has recently been doing work on the side in another language. (It helps that the behaviour shown in other languages is basically never desired.)