Yet Another Switch-Case Syntax Proposal

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Apr 4 04:33:28 EDT 2014


On Thu, 03 Apr 2014 11:23:39 -0700, Ethan Furman wrote:

> On 04/03/2014 09:02 AM, Lucas Malor wrote:
>>
>> In reply to Ian Kelly:
>>>
>>> Instead of disabling fallthrough by default, why not disable it all
>>> together?
>>
>> I was tempted but there are cases in which it's useful. An example
>>
>> switch day casein ("Monday", "Thursday", "Wednesday", "Tuesday",
>> "Friday"):
>>      gotowork = True
>>      continue
>> casein ("Monday", "Thursday", "Wednesday", "Tuesday", "Friday"):
>>      daytype = "ferial"
>> casein ("Saturday", "Sunday")
>>      daytype = "festive"
> 
> 
> Absolutely not.  Currently, the 'continue' key word means "stop
> processing and go back to the beginning".  

It does not go back to the beginning. That would require resetting the 
iterable, which may be impossible. And if you succeeded, it would mean a 
loop with continue might never terminate!

for i in (1, 2, 3):
    print(i)
    if i == 2: continue

=> prints:
1
2
1
2
1
2
1
...


Perhaps you mean "continue from the top"? But top and bottom is just an 
artifact of how we write the loop. The only direction that matters is 
iteration order, and I'm not aware of any language that allows for-loop 
iteration to go forward and backwards.

"continue" means "continue with the next iteration", as the documentation 
says:

https://docs.python.org/3/reference/simple_stmts.html

In some languages, it's even spelled "next", e.g. Perl. (After a decade 
plus of reading and writing Python code, I still try to write "next" when 
I mean continue.)  It's not a big stretch to go from "continue with the 
next iteration" to "continue with the next case".


> You would have it mean "keep
> going forward".  Thus 'continue' would mean both "go backwards" and "go
> forwards" and would lead to unnecessary confusion.

Well, there's certainly already some confusion :-)


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/



More information about the Python-list mailing list