Comparisons
Comparison operations are supported
by all objects. They have the same priority (which is higher than
that of the boolean operations). Comparisons can be chained
arbitrarily, e.g., x
<
y
<=
z is
equivalent to x
<
y and y
<=
z, except that
y is evaluated only once (but in both cases
z is not evaluated at all when
x
<
y is
found to be false).
The following table summarizes the comparison operations.
|
Operation |
Meaning |
Notes |
< |
Strictly less than | |
<= |
Less than or equal | |
> |
Strictly greater than | |
>= |
Greater than or equal | |
== |
Equal | |
<> |
Not equal |
1 |
!= |
Not equal |
1 |
Is |
Object identity | |
is not |
Negated object identity |
Notes
<> and != are alternate spellings for the same operator. (We couldn’t choose between ABC and C!)
Objects of different types, except different numeric types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). Furthermore, some types (e.g., windows) support only a degenerate notion of comparison where any two objects of that type are unequal. Again, such objects are ordered arbitrarily but consistently.
Implementation note: objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
Two more operations with the same syntactic priority,
"in" and "not
in", are supported only by sequence types, see the
later section Sequence Types .
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access