for / while else doesn't make sense
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jun 7 04:36:58 EDT 2016
On Tuesday 07 June 2016 17:52, Lawrence D’Oliveiro wrote:
> On Tuesday, June 7, 2016 at 4:36:37 PM UTC+12, Ian wrote:
>> A 500-line function? Yikes, what an eyesore. When you have to include
>> #end comments in order to visually match things up, that should be a
>> smell that your code is excessively complex.
>
> Feel free to come up with a simpler version.
If I could work out what your convoluted version is supposed to do, I might
give it a try.
[...]
>> def generate_lines():
>> nonlocal input_line
[snip code]
> Wow, that’s only twice the length of the code you’re replacing. Well done.
I count 18 lines in your version using while loops, excluding comments, but
including placeholder ... lines, compared to 17 lines in Ian's version. How do
you get "twice the length"?
Ian's version is also much simpler: there are no breaks and no variables being
assigned to None so you can detect the end of the loop, and only three `if`s
instead of six in your version. That makes Ian's objectively less complex than
your example, assuming it does the same thing. (Although I'm a bit dubious
about the use of nonlocal.)
--
Steve
More information about the Python-list
mailing list