Proposed PEP for a Conditional Expression

James_Althoff at i2.com James_Althoff at i2.com
Fri Sep 14 14:21:31 EDT 2001


With apologies for possibly having missed some parts of this thread . . .

Given:
1) nested scopes, and
2) recent proposals becoming more and more complicated and convoluted --
e.g., embedding "break" and "return" statements inside if:else: expression
syntax, highly overloaded meanings of if:else: ,
inside/outside/upside/downside ordering of if:else: keywords, etc., . . .

Why not just start with something simple like a builtin "cond" function
approximating the following:

Python 2.2a1 (#21, Jul 18 2001, 04:25:46) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>>
>>> from __future__ import nested_scopes
>>>
>>> def cond(expr, iftrue, iffalse=lambda:None):
...   if expr: return iftrue()
...   return iffalse()
...
>>> x = -2
>>> abs = cond(x>=0, lambda:x, lambda:-x)
>>> abs
2
>>> l = [7,8,9]
>>> firstItem = cond(len(l)>0, lambda:l[0])
>>> firstItem
7
>>> l = []
>>> firstItem = cond(len(l)>0, lambda:l[0])
>>> print firstItem
None
>>>

Seems like this would handle many of the common, simple cases.

Jim

ps.  Then attention could be focused on defining a mechanism for
general-purpose, unnamed, "in-place" code blocks.  If we had such, we could
then create many convenient idioms using functions and methods -- instead
of constantly wrangling with new syntactic forms and keywords.






More information about the Python-list mailing list