code review

Steven D'Aprano steve+comp.lang.python at
Fri Jul 13 17:04:21 CEST 2012

On Fri, 13 Jul 2012 12:30:47 +0000, Albert van der Horst wrote:

>>Apart from Python, Mathematica, Perl 6, CoffeeScript, Cobra and Clay
>>give chained comparisons the standard meaning. It is, or was, a feature
>>request for Boo, but I can't tell whether it has been implemented or
> Algol 68 does not. It has promoted operator symbols to first class
> citizens. In that context chained comparison operators cannot be made to
> work.
> Both Mathematica and Perl are ad-hoc-ish languages. I would hate Python
> go that way.

Perl 5 does not have chained comparisons. Perl 6, which is more designed 
and less ad-hoc, does.

> From now on, for each operator I would have to remember wether it is a
> supposedly comparison operator or not.

Are you often in the habit of using operators *without* remembering what 
they do? <wink>

I can't speak for you, but it isn't often that I've found myself not 
remembering whether the less-than operator <  means "compare two values" 
or "multiply two values".

> Comparison operations on booleans make sense.

Actually, no. Is True less than False, or is it greater? In boolean 
algebra, the question has no answer. It is only an implementation detail 
of Python that chooses False < True.

Of course, equality and inequality make sense for all values. But the 
other comparison operators, < <= >= > only make sense for values which 
are ordered, like real numbers (but not complex numbers), strings, and 
similar, or for set comparisons (subset and superset). Arbitrary types 
may not define comparison operators.

> I remember a FORTRAN
> compiler complaining about me comparing LOGICALS. The lack of
> abstraction!

The opposite: LOGICALS are abstract values which do not define greater 
than or less than.

> The worst of is, of course, = for assignment instead of := . This is a
> convention that Python follows, to my dismay.


The worst is to use = for both equality and assignment, like some BASICs. 
At least Python does not allow assignment as an expression, so you can't 
make the typical C error of:

if x = y: do_something()  # oops meant x == y


More information about the Python-list mailing list