[Python-Dev] Conditional For Statements (original) (raw)
Ryan Hitchman hitchmanr at gmail.com
Mon May 19 03:45:07 CEST 2008
- Previous message: [Python-Dev] buffer interface for C extensions
- Next message: [Python-Dev] Conditional For Statements
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message: [Python-Dev] buffer interface for C extensions
- Next message: [Python-Dev] Conditional For Statements
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]