[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:

jimjhb at aol.com jimjhb at aol.com
Tue Jun 25 20:22:12 CEST 2013


Yeah, that's basically it.  Except it's clear the other members of this list understand subtle python grammar MUCH better than I do.


Main issue seems to be that programmers "shouldn't" shy away from 'break' (I can kind of see the argument both ways) but they do.  So a lot of people are making their code more confusing...


I think for deserves a 'while' because it is really sparing in its implementation (no index, no explicit bounds check) and so deserves a clean way to prematurely terminate the loop if needed.  Break is a little harsh (for forbidden by many), itertools.takewhile is too wordy, and anything else is worse....


I think prematurely terminating a for loop is a very common activity as well.


-Jim 



-----Original Message-----
From: David Mertz <mertz at gnosis.cx>
To: python-ideas <python-ideas at python.org>
Sent: Tue, Jun 25, 2013 12:55 pm
Subject: Re: [Python-ideas] Is this PEP-able? for X in ListY while conditionZ:



I'm not quite certain if this is what the OP is proposing, but I think that extending comprehensions to allow a 'while' clause would be intuitive and somewhat useful.  It's true that itertools.takewhile() basically gets us the same thing, but actual syntax would be nice, and also more straightforward for comprehensions other than generator comprehensions.  E.g. 


  attendees = {guest:guest.plus_N for guest in waiting_list while not room_full()}


This would actually produce the same result as:


  attendees = {guest:guest.plus_N for guest in waiting_list if not room_full()}


But it would save the extra looping over a bunch of final False values of 'room_full()'.



On Tue, Jun 25, 2013 at 6:35 AM,  <jimjhb at aol.com> wrote:

Syntax:





for X in ListY while conditionZ:


The 'for' loop would proceed as long as conditionZ remains true.


The motivation is to be able to make use of all the great aspects of the python 'for' (no indexing or explicit
end condition check, etc.) and at the same time avoiding a 'break' from the 'for'.  


(NOTE:  Many people are being taught to avoid 'break' and 'continue' at all costs, so they instead convert
the clean 'for' into a less-clean 'while'.  Or they just let the 'for' run out.  You can argue against this teaching
practice (at least for Python) but that doesn't mean it's not prevalent and prevailing.)


[People who avoid the 'break' by functionalizing an inner portion of the loop are just kidding themselves and making their own code worse, IMO. Takewhile from itertools also works, but that's clumsy and wordy as well.]


I'm not super familiar with CPython, but I'm pretty sure I could get this up and working without too much effort.


Please note that I don't feel the answer to this is 'just use break'.  Programmers are now being taught to avoid 'break' and 'continue' as if they were 'goto's.  The result (now) is that people are avoiding the 'for' (with its GREAT properties) because they can't break out of it.


Comments and Questions welcome.


Thanks.  



_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
http://mail.python.org/mailman/listinfo/python-ideas





-- 
Keeping medicines from the bloodstreams of the sick; food 
from the bellies of the hungry; books from the hands of the 
uneducated; technology from the underdeveloped; and putting 
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.




_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
http://mail.python.org/mailman/listinfo/python-ideas

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130625/12c79485/attachment.html>


More information about the Python-ideas mailing list