File TXT tidak ditemukan.
File TXT tidak ditemukan.
Best hidden feature of Python | Chaining comparison operators
HPfPFM1wNmE • 2020-08-21
Transcript preview
Open
Kind: captions
Language: en
this is a hidden feature of python that
i recently came across
the chaining of comparison operators
that is not available in almost
any mainstream programming language i
think it's elegant and intuitive and
doesn't make any sense to me why it's
not available in most languages
so what is it say we assign the values 2
and 3 to x and y
and then look at a single statement that
includes several comparison operators
chained together
1 less than x less than y less than 4
in python this evaluates to true the way
python evaluates the statement is the
same way that we would
intuitively or mathematically look at
the statement
which is as a chain of binary comparison
operators
1 is less than x and x is less than y
and y is less than 4 which again
evaluates to true now you can use any
comparison operator
less than greater than less than or
equal to greater than or equal to and
mix and match them together in a single
arbitrarily long chain of comparison
operators
now if we change the original statement
to include a greater than operator is
the last comparison
then the entire statement returns false
because y which is equal to 3
is not greater than 4. and then finally
again we can flip the 4 and the y
to make the statement return true
because all the individual comparisons
are true 1 is less than x which is equal
to two
x is less than four and four is greater
than y
y being equal to three now this feature
is available in a few
other languages not many like pearl six
or reiku i think it's been her name too
not sure how to pronounce it
and julia and as shown here it's also
a first-class citizen in some functional
languages like scheme common lisp and
closure
with the added constraint that the
chaining of the operator includes only
the same operator so you can't mix and
match so shown here the greatest
language of all time which is lisp
the equals operator applied to a list of
numbers
three and three returns true three and
five returns false
all threes returns true all threes
except one of them being five returns
false
so again that's chaining the equality
comparison operator across the entire
list
and the same is true for the less than
operator applied to the entire list
below 3 less than 5 is true
and then a long sequence returning true
if it's in strictly increasing order
and false if it's not in strictly
increasing order
i put some links in the description one
of the more interesting ones is in the
software engineering stack exchange
which discusses from a
semi-philosophical perspective
why most mainstream languages do not
include this feature
you should check out some of the answers
on that page but to summarize some of
the discussion the reason to do it
is despite the initial intuition about
this feature being difficult to
implement
it's actually very easy to implement and
as i said it's a
mathematically intuitive and just
elegant statement which i think makes it
one of the
best hidden features at least to me of
python
in the discussion the reasons that come
up not to do it is
fundamentally just laziness in that its
importance versus other features is
quite low
and it doesn't seem to be the kind of
feature that pops up as an
intuitive first feature to implement
when the language is first born and
as with certain other features this can
potentially break backward compatibility
if this kind of chaining operators was
allowed previously
syntactically speaking meaning it was
allowed but it
didn't do the intuitive thing it can
certainly break in quite painful ways
backward compatibility
but still as i said in the previous
video list comprehensions i think is the
best feature python
and the chaining of comparison operators
i think
is the best hidden feature or not well
known feature
of python quick shout out to expressvpn
click their link in the description
it's the best way to support the podcast
i host in these videos that i make
if you enjoy these subscribe and
remember try to learn something new
every day
you
Resume
Read
file updated 2026-02-13 13:24:12 UTC
Categories
Manage