[Python-3000] Stackable Blocks
Nick Coghlan
ncoghlan at gmail.com
Mon Apr 24 04:27:01 CEST 2006
Crutcher Dunnavant wrote:
> Having been shot down so hard on my request to bring for loops into
> symetry with list comprehensions, I have a new proposal - stackable
> blocks.
>
> This is a very _small_ amount of syntax sugar, the basic idea is that
> any block headers can be stacked on the same line. This changes no
> semantics, nor does it change ordering, or add new keywords, it just
> changes a tiny bit of the grammar for whitespace/block parsing.
What's wrong with a bit of vertical whitespace? If your code blocks are
cascading to the right of screen, it's a sign that something needs to be
pulled out into a separate function or generator.
> can be spelled like this.
>
> for file in open_files: if file.readable():
> ...
def readable_files(files):
for f in files:
if f.readable():
yield f
for f in readable_files(open_files):
...
> can be spelled:
>
> for i in range(10): for j in range(20): if i != j:
> ...
def cartesian(x_coords, y_coords):
for x in x_coords:
for y in y_coords:
yield x, y
for i, j in cartesian(range(10), range(20)):
if i != j:
...
> can be this:
>
> for line in open(file): if line.strip():
> ...
def stripped(f):
for line in f:
yield line.strip()
for line in stripped(f):
...
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list