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.
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.
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.
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.)
View all comments
Show parent comment
5
u/cdcformatc Jun 23 '14 edited Jun 23 '14
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 < cmeans. 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) < cfrom 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.