[Python-Dev] Conditional For Statements

2008-05-18 Thread Ryan Hitchman
I'd like to propose an addition to the for statement's syntax:

for {variable} in {iterable} if {condition}:
{block}

which is equivalent to

for {variable} in {iterable}:
if not {condition}:
continue
{block}

and

for {variable} in filter(lambda: {condition}, iterable):
{block}

This would make the syntax closer to that of generators, which have
'for variable in iterable if condition', and would improve code
clarity by increased brevity and not negating boolean expressions.

Following are examples of current code with what the new code would
be, taken from the Python 3.0a5 tarball.

Demo/tkinter/guido/ss1.py:163:
for (x, y), cell in self.cells.items():
if x <= 0 or y <= 0:
continue
for (x, y), cell in self.cells.items() if x > 0 and y > 0:

Lib/encodings/__init__.py:91:
for modname in modnames:
if not modname or '.' in modname:
continue
for modname in modnames if modname and '.' not in modname:

Lib/idlelib/AutoExpand.py:70:
for w in wafter:
if dict.get(w):
continue
for w in wafter if w not in dict:

Lib/Cookie.py:483:
for K,V in items:
if V == "": continue
if K not in attrs: continue
for K,V in items if V != "" and K not in attrs:

Lib/hashlib.py:108:
for opensslFuncName in filter(lambda n: n.startswith('openssl_'),
dir(_hashlib)):
for opensslFuncName in dir(_hashlib) if opensslFuncName.startswith('openssl_'):

There are many more examples of this in the standard library, and
likely even more in production code.

I am not familiar with LL(1) parsing, so this may impossible under
that constraint.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Conditional For Statements

2008-05-19 Thread Ryan Hitchman
On Sun, May 21, 2006 at 17:38:49 CEST, Guido van Rossum  wrote
> ...
> Also, it would be a parsing conflict for the new conditional
> expressions (x if T else y).
> ...

That's all I needed to know.

Sorry, everyone, I'll try not to waste your time in the future.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com