[Python-Dev] Conditional For Statements

Eli Courtwright eli at courtwright.org
Mon May 19 03:57:24 CEST 2008


First, the best list on which to discuss this would be Python-Ideas not
Python-Dev.

Second, this was brought up there a few months ago, although not much
discussion ensued:
http://mail.python.org/pipermail/python-ideas/2008-March/001407.html

- Eli


On Sun, May 18, 2008 at 9:45 PM, Ryan Hitchman <hitchmanr at gmail.com> wrote:

> 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 at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/eli%40courtwright.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20080518/d94aab22/attachment.htm>


More information about the Python-Dev mailing list