FW: Switch statements again

Carl Banks imbosol at vt.edu
Thu Jan 16 15:36:18 EST 2003


Mike Meyer wrote:
> Carl Banks <imbosol at vt.edu> writes:
> 
>> Personally, the elifs don't bother me in the least.  In Python, the
>> switches are likely to make indentation a pain; it's not clear whether
>> case tags should be indented or not.  If they are, you have two
>> indentation levels for the code inside.  If they aren't, you've broken
>> a fundamental property of indentation.
> 
> Why does not indenting the case tags break a fundamental property of
> indentation? Why is something like (using syntax suggested in PEP
> 275):
> 
> when my_value:
>    # Can we do something useful with this?
> in (...):
>    code
> in (...):
>    more code
> else:
>    yet more code
> 
> Any worse than:
> 
> if my_value in (...): code
> elif my_value in (...):
>    more code
> else:
>    more code

Yes.  All of the in statements look back to a single when.  An elif
looks only back to the previous if/elif.  The in statements fall
"inside" the when statement.  The elif falls "after" the previous
if/elif.  So, unindented ins is worse than unindented elifs: that
which fall inside tags ought to be indented.

However, the former is NOT any worse than try followed by a bunch of
excepts.  All the excepts look back to the same try.  Conceptually,
the excepts should be nested inside the try statement, but I (and
everyone else) have been happily using them where they are.

For that reason, I retract my statement.


-- 
CARL BANKS




More information about the Python-list mailing list