On 16/04/2017 03:51, Steve D'Aprano wrote:
On Sat, 15 Apr 2017 10:17 pm, bartc wrote:
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).
That's the advantage of writing your own private language and having no
users except for yourself. You get to cut corners. Python has tens of
thousands of users, and doesn't have that luxury.
Here are the lines of code in my C compiler which are necessary to
support 'while': https://pastebin.com/BYFV7EWr
(45 lines shown, but there are exactly 30 Loc if blanks are excluded.)
I'd be interested in knowing why implementing While in Python would need
significantly more. (The 30 Loc figure is with support for loops /in
general/ already in place, and is for /adding/ a new loop statement, in
this case 'while')
For language developers with responsibilities to users, the job doesn't stop
at just implementing the feature in the language. It also needs to be
tested and documented.
As a general rule of thumb, every line of production code should expect to
add at least ten lines of test code (unit tests, regression tests,
integration tests, functional tests, doc tests, etc). So for your "30
lines" feature, that adds 300 lines of tests, and probably another page or
two of documentation:
No, we're talking about a loop. It must be just about the simplest thing
to implement in a language (compared with a type system, or code
generation).
BTW supporting a dedicated endless loop was only about 20 lines in
another project. Believe me, it is /nothing/. I wish other aspects were
just as trivial. It didn't even need a dedicated keyword.
> - describing the various syntax forms;
> - explaining how they differ;
> - tutorials for beginners showing each form;
And you don't have to explain how an endless loop should be written as
'while True', meanwhile advising against using 'while 1'?
The more choices you offer, the harder that decision becomes:
- numeric Pascal or C-style loop
- foreach style loop
- repeat while condition (test at start)
- repeat until condition (test at start)
- do ... while condition (test at end)
- do ... until condition (test at end)
- repeat forever
So, how many kinds of sequences do you have in Python?
lists
tuples
namedtuples
arrays
bytearrays
string ?
memoryview
plus all those ordered types. My head is already spinning!
The loops I use are categorised as:
* Endless
* N times
* Until some condition is true
* Iterate over an integer sequence
* Iterate over a set of values of some object
Other languages like to have even more elaborate schemes. That includes
advanced uses of Python's for loop, were, for example, there are
multiple loop variables.
I wonder how much testing that took to get it right?
I don't remember the language, but I remember seeing one generalisation of
the repeat/do loop that puts the test in the middle, rather than at the
start or end of the loop. If I remember it was something like:
DO
setup code # executed once only
REPEAT
loop body # before the test
WHILE condition # test
loop body # after the test
thus combining both the
repeat while condition:
...
and
do:
...
until condition
styles of looping in one handy syntax.
Perhaps you misremembered as that looks too unwieldy to be practical. If
implemented orthogonally, then a loop like this:
While A Do B End
where both A and B can be any sequence of statements or expressions,
would allow you to write:
While a; b; c; d Do e; f; g End
with the test (the value of 'd') effectively in the middle.
But very common requirements are endless loops, and repeat N times
without needing an explicit counter.
If by "very common" you mean "occasionally", I agree.
Most of my loops start off as endless loops, until I can determine the
actual terminating condition, and where it best goes. Sometimes they
stay as endless loops.
(Sometimes, I turn a normal statement into an endless loop with a 'do'
prefix. This is an unusual feature but I use it quite a bit:
doswitch nextchar() # looping version of 'switch'
when 'A'..'Z' then ....
else exit # ie. break out of loop
end
)
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.
The problem there is not the loop, but the foolish use of lowercase l as a
variable name.
Maybe. But if 'while 1' wasn't used, then that's one less thing to
double-check. (I just looked at a random bunch of Python code; there
were 25 instances of 'while True:', but 40 of 'while 1:'.)
--
bartc
--
https://mail.python.org/mailman/listinfo/python-list