back to index

Best hidden feature of Python | Chaining comparison operators


Whisper Transcript | Transcript Only Page

00:00:00.000 | This is a hidden feature of Python that I recently came across,
00:00:04.240 | the chaining of comparison operators that is not available in almost any mainstream programming
00:00:09.920 | language. I think it's elegant and intuitive and doesn't make any sense to me why it's not
00:00:14.640 | available in most languages. So what is it? Say we assign the values 2 and 3 to x and y,
00:00:19.920 | and then look at a single statement that includes several comparison operators chained together,
00:00:26.000 | 1 less than x less than y less than 4. In Python this evaluates to true. The way Python evaluates
00:00:33.040 | the statement is the same way that we would intuitively or mathematically look at the
00:00:37.600 | statement, which is as a chain of binary comparison operators. 1 is less than x and x is less than y
00:00:46.800 | and y is less than 4, which again evaluates to true. Now you can use any comparison operator,
00:00:54.000 | less than, greater than, less than or equal to, greater than or equal to, and mix and mash them
00:00:58.880 | together in a single arbitrarily long chain of comparison operators. Now if we change the
00:01:04.720 | original statement to include a greater than operator as the last comparison, then the entire
00:01:09.600 | statement returns false because y which is equal to 3 is not greater than 4. And then finally again
00:01:16.720 | we can flip the 4 and the y to make the statement return true because all the individual comparisons
00:01:22.720 | are true. 1 is less than x which is equal to 2, x is less than 4, and 4 is greater than y,
00:01:30.240 | y being equal to 3. Now this feature is available in a few other languages, not many, like Perl 6
00:01:37.200 | or Reiku, I think it's been renamed to, not sure how to pronounce it, and Julia. And as shown here
00:01:44.080 | it's also a first class citizen in some functional languages like Scheme, Common Lisp, and Closure,
00:01:50.560 | with the added constraint that the chaining of the operator includes only the same operator,
00:01:55.200 | so you can't mix and match. So shown here the greatest language of all time which is Lisp,
00:01:59.280 | the equals operator applied to a list of numbers, 3 and 3 returns true, 3 and 5 returns false,
00:02:07.360 | all 3s returns true, all 3s except one of them being 5 returns false. So again that's chaining
00:02:14.080 | the equality comparison operator across the entire list. And the same is true for the less than
00:02:20.000 | operator applied to the entire list. Below 3 less than 5 is true, and then a long sequence returning
00:02:26.960 | true if it's in strictly increasing order, and false if it's not in strictly increasing order.
00:02:32.960 | I put some links in the description. One of the more interesting ones is in the
00:02:37.840 | Software Engineering Stack Exchange which discusses from a semi-philosophical perspective
00:02:42.720 | why most mainstream languages do not include this feature. You should check out some of the answers
00:02:47.280 | on that page. But to summarize some of the discussion, the reason to do it is despite the
00:02:52.240 | initial intuition about this feature being difficult to implement, it's actually very easy
00:02:56.800 | to implement. And as I said it's a mathematically intuitive and just elegant statement which I think
00:03:04.880 | makes it one of the best hidden features, at least to me, of Python. In the discussion the reasons
00:03:11.520 | that come up not to do it is fundamentally just laziness in that its importance versus other
00:03:17.440 | features is quite low, and it doesn't seem to be the kind of feature that pops up as an intuitive
00:03:23.200 | first feature to implement when the language is first born. And as with certain other features
00:03:30.640 | this can potentially break backward compatibility if this kind of chaining operators was allowed
00:03:35.920 | previously, syntactically speaking, meaning it was allowed but it didn't do the intuitive thing,
00:03:41.840 | it can certainly break in quite painful ways backward compatibility. But still, as I said
00:03:46.640 | in the previous video, list comprehensions I think is the best feature of Python, and the
00:03:51.760 | chaining of comparison operators I think is the best hidden feature, or not well-known feature,
00:03:57.760 | of Python. Quick shout out to ExpressVPN. Click their link in the description. It's the best way
00:04:04.560 | to support the podcast I host and these videos that I make. If you enjoy these, subscribe,
00:04:10.000 | and remember, try to learn something new every day.
00:04:28.460 | [BLANK_AUDIO]