switch

MRAB python at mrabarnett.plus.com
Wed Dec 9 15:13:30 EST 2009


Tim Chase wrote:
> Carl Banks wrote:
>> What if the object is a string you just read from a file?
>>
>> How do you dispatch using polymorphism in that case?
> 
[snip]
> 
> which would nicely change into something like
> 
>   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.



More information about the Python-list mailing list