On Tue, Apr 22, 2014 at 10:37 AM, Lucas Malor <7vsfeu4pxg@snkmail.com> wrote:
switch tarot case 0:
    card = "Fool"
case 1:
    card = "Alan Moore"
case 2:
    card = "High Priestess"
<etc....>

-100.

I have to say "yuck" to this sort of thing; it feels entirely unpythonic. Each 'case' is some sort of comparison, but you can't tell locally what is being compared.  Of course in the simple one-line blocks it's easy enough to glance up to the 'switch', but if you had 20-30 lines of code within each 'case' you could look at 'case 20' and not remember/know what is being switched on.

Using the Case class idea seems much more clear, as well as being much more flexible.  It's true the initialization of the instance might be a ways up, but at least you know that it's an argument passed to a callable, so you already *know*  that you'd have to look at the creation of that callable.  Moreover, being generalizable is nice, e.g. (with a slightly enhanced class, implementation left to the reader):

    case = Case(my_color(some_complex_expression(this, that, other) + more_stuff(foo, bar))
    if case('red'):
        some_red_stuff()
    elif case.in('green', 'blue'):
        some_bluegreen_stuff()
    elif '#00AAAA' < case < '#EEBBAA':  # some definition of color range
        something_with_color_range()
    else:
        paint_it_black()

Of course, in my example, maybe a better name for the 'case' instance is actually 'color'.  The fact I can choose a name that fits the context adds even more clarity over all "cases" needing to be named 'case'.
 


--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.