attaching names to subexpressions

Neil Cerutti neilc at norwich.edu
Mon Oct 29 07:53:52 EDT 2012


On 2012-10-28, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
>>> The 'canonical way'
>>> while True:
>>>      line = complex_expression
>>>      if not line:
>>>          break
>>>      do_something_with(line)
>>>
>>> avoids this problem, but I was never really convinced about the beauty /
>>> readbility of this construct.
>>>
>>> In
>>> my opinion I shouldn't be obliged to read any of the indented lines of
>>> the while statement on a first 'visual' pass through somebody elses code
>>> and still be able to see what the loop iterates through.
>>
>> Fine. Then write your code as:
>>
>> line = function(x, y, z)
>> while line:
>>      do something with(line)
>>      line = function(x, y, z)
>
> We have a problem, and two solutions. Solution 1 has downside
> A, and solution 2 has downside B. If he complains about
> downside A, you say, well, use solution 2. If he complains
> about downside B, you say, well, use solution 1.
>
> What if he wants to avoid both downsides A and B? What solution
> does he use then?

You abandon the while loop and compose a generator.

-- 
Neil Cerutti



More information about the Python-list mailing list