PEP308 Late Entry
Clark C. Evans
cce at clarkevans.com
Sun Mar 9 21:11:40 CET 2003
On Sat, Mar 08, 2003 at 10:35:00AM +0000, Stephen Horne wrote:
| select : (c1 : r1; c2 : r2; r3)
| select s : (v1 : r1; v2 : r2; r3)
| select sa, sb : (v1a, v1b : r1; v2a, v2b : r2; r3)
| 's' stands for 'switch expression'.
| 'c' for condition.
| 'r' for result.
| 'v' for switch-value.
Ok. But I think 'when' adds readability, and you don't need
a separate case for your third example.
a = select when c1 : r1; when c2 : r2; else r3
b = select s when v1 : r1; when v2 : r2; else r3
c = select (sa,sb) when (v1a, v1b): r1; when (v2a, v2b): r2; else r3
Or, written in multi-line form..
a = select
when c1: r1
when c2: r2
b = select s
when v1: r1
when v2: r2
c = select (sa,sb)
when (v1a, v1b): r2
| Switch values could even support special syntaxes such as relative
| operator prefixes ('<', '>' etc) or perhaps a '..' or 'to' notation
| for ranges - though the non-switch first form might be clearer.
I'd call it 'select' and not 'switch'; the 'switch' expression
in "C" is a statement-level operation, while this proposal seems
still to be an expression.
And yes, I like the operator prefixes:
when > 100: 'huge'
when > 50 : 'big'
when > 30 : 'medium'
when > 20 : 'respectable'
when > 10 : 'small'
when 'UP': 'Upper Right'
when 'DOWN': 'Lower Right'
else: 'Center Right'
when 'UP': 'Upper Left'
when 'DOWN': 'Lower Left'
else: 'Center Left'
when 'UP': 'Upper Middle'
when 'DOWN': 'Lower Middle'
else: 'Center Middle'
| 1. It handles 'elif' simply and succinctly.
| 2. It supports the switch/case functionality as a simple extension,
| but unlike the earlier proposal does not have to be split over
| multiple lines unless clarity genuinely demands it.
I don't mind being able to add a ';' to make it a one-line
construct if it is necessary.
| 3. The use of punctuation keeps it succinct while the initial keyword
| makes it clear what is happening (easy to look up in documentation
I think the parenthesis is ugly.
| 4. The initial ':' and the use of ':' and ';' in the parentheses
| makes it clear that this is not a function, for those who see
| function-call notation as implying no shortcircuiting.
Yes, the ; is nice.
More information about the Python-list