switch

Tim Chase python.list at tim.thechases.com
Wed Dec 9 16:09:29 EST 2009


MRAB wrote:
> Tim Chase wrote:
>>   switch row['recordtype']:
>>     case '01':
>>       phone.international += Decimal(row['internationalcost'])
>>       // optionally a "break" here depending on
>>       // C/C++/Java/PHP syntax vs. Pascal syntax which
>>       // doesn't have fall-through
>>     case '02':
>>       phone.text_messaging += (
>>         int(row['textmessages sent']) +
>>         int(row['pages received']) +
>>         int(row['textmessages sent']) +
>>         int(row['pages received'])
>>     ...
>>     default:
>>       raise WhatTheHeckIsThis()
>>
>> This doesn't convert well (i.e. compactly) to a dictionary-dispatch 
>> idiom. :(
>>
> Shouldn't 'case' be indented to the same level as 'switch'? And
> 'default' could be replaced by 'else' without ambiguity.

But I want a GREEN bike-shed! :-)  Yeah, "else" works nicely and 
makes sense.  Indentation could go either way in my book, but I 
lean towards indented "case" because the "switch" can get easily 
lost if the "case"s aren't indented:

   switch foo:
   case 1:
     stuff()
   case 2:
     morestuff()
   switch bar:
   case 3:
     whatever()
   case 4:
     yet_more()
   else:
     whip_it()

vs

   switch foo:
     case 1:
       stuff()
     case 2:
       morestuff()
   switch bar:
     case 3:
       whatever()
     case 4:
       yet_more()
     else:
       whip_it()

Just my ponderings...

-tkc






More information about the Python-list mailing list