Case Statements
BartC
bc at freeuk.com
Wed Mar 16 08:53:50 EDT 2016
On 16/03/2016 12:21, Marko Rauhamaa wrote:
> BartC <bc at freeuk.com>:
>> That's the first time I've heard a language feature common in C
>> described as sexy.
>
> Scheme has a "switch" statement (a "case" form). However, it is slightly
> better equipped for it than Python:
>
> * Scheme has an atom type ("symbol"). It corresponds to interned
> strings and is supposed to be compared by reference.
>
> * Scheme has defined three equality operators: "eq?", "eqv?" and
> "equal?". Python only has two: "is" (~ "eq?") and "==" (~ "equal?").
> The "case" form makes use of the operator "eqv?" that is missing from
> Python ("eqv?" compares numbers numerically but is otherwise the same
> as "eq?").
>
Yes, a few scripting languages can do interesting things with switch or
case statements. Perl for example (where I think it is created out other
language features, but it looks a regular part of the syntax).
Even Ruby has one. It doesn't do anything 'sexy' with it, but it does
have this:
case
when this
....
when that
....
when other
...
end
which is exactly equivalent to if this... elif that... (when the tests
are ordered), with one difference:
Each test starts with "when", instead of "if" for the first and "elif"
for subsequent ones. That makes it easier to reorder tests, temporarily
comment out the first test, copy a test from elsewhere, insert a new
first test (you get the idea).
--
Bartc
More information about the Python-list
mailing list