[Python-Dev] extended if and while statements
Raymond Hettinger
python at rcn.com
Tue Jan 27 23:07:42 EST 2004
> if book_count() ..
> == 1: purchase_9_books()
> == 2: purchase_8_books()
> < 5: purchase_5_books()
> else:
> rearrange_bookshelves()
When the action clause is listed on a separate line (as it should be)
as it must be for multi-line blocks, the result is not as pretty
and shows that an additional level of indentation is required with
almost no pay-off:
if book_count() ..
== 1:
purchase_9_books()
== 2:
purchase_8_books()
< 5:
purchase_5_books()
else:
rearrange_bookshelves
I don't see this as a win over the following equivalent:
> nBooks = book_count()
> if nBooks == 1:
> purchase_9_books()
> elif nBooks == 2:
> purchase_8_books()
> elif nBooks < 5:
> purchase_5_books()
> else:
> rearrange_bookshelves()
I recommend teasing out the idea on comp.lang.python.
There will be no shortage of criticisms on every possible
variation. There have been many suggestions for a switch
statement and a couple of them were elegant and pythonic.
Here is one variant:
select book_count():
case 1:
purchase_9_books()
case 2:
purchase_8_books()
case < 5:
purchase_5_books()
else:
rearrange_bookshelves
Here is a link to one of the many threads on ideas for
python switch-case statements:
http://groups.google.com/groups?th=1fafecbd71f20d27
Raymond Hettinger
More information about the Python-Dev
mailing list