attaching names to subexpressions

Devin Jeanpierre jeanpierreda at gmail.com
Sun Oct 28 01:57:45 EDT 2012


On Sat, Oct 27, 2012 at 8:49 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Sat, 27 Oct 2012 15:03:38 +0200, Gelonida N wrote:
>
>> line = complex_exression
>> while line:
>>      do something with(line)
>>      line = complex_expression
>>
>> violates clearly the DRY principle  and the risk to change one line but
>> not the other is high.
-- snip --
> while True:
>      line = function(x, y, z)
>      if not line:
>          break
>      big ugly block of code
>
>
> There you go. DRY.

>> 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?

-- Devin



More information about the Python-list mailing list