Python and the need for speed
bartc
bc at freeuk.com
Sat Apr 15 08:17:45 EDT 2017
On 15/04/2017 03:35, Rick Johnson wrote:
> On Wednesday, April 12, 2017 at 8:44:30 AM UTC-5, bart... at gmail.com wrote:
> At a minimum, every language should offer
> the following four loop-forms (using Python semantics):
>
> while CONDITION:
> doSomething()
>
> for VALUE in COLLECTION:
> doSomething(value)
>
> loop(N):
> doSomething()
>
> loop(N) as i:
> doSomething(i)
>
Yes, I'm constantly surprised at this, as such syntax has a very low
cost (in my last compiler, supporting 'while' for example only added 30
lines to the project).
Of course, it's possible to overdo it; if you look at Lisp, you'll lose
yourself in the myriad looping options.
But very common requirements are endless loops, and repeat N times
without needing an explicit counter. The former /can/ be easily written as:
while 1:
body
but it's more psychological; I don't want to use an idiom to denote an
endless loop, I want to be able to express it directly!
Python's byte-code does at least optimise out the check that '1' is
true, but that's not what the reader sees, which is 'loop while 1 is
true'. And one day it will be:
while l:
body
that can be mistaken for that common idiom.
--
bartc
More information about the Python-list
mailing list