Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32362

Re: attaching names to subexpressions

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: attaching names to subexpressions
Date 2012-10-29 11:53 +0000
Organization Norwich University
Message-ID <af792fF490mU2@mid.individual.net> (permalink)
References <a533d812-480e-479b-9210-7097c2d70d73@a4g2000pbo.googlegroups.com> <mailman.2934.1351343030.27098.python-list@python.org> <508c810f$0$29967$c3e8da3$5496439d@news.astraweb.com> <mailman.2950.1351403908.27098.python-list@python.org>

Show all headers | View raw


On 2012-10-28, Devin Jeanpierre <jeanpierreda@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

attaching names to subexpressions Steve Howell <showell30@yahoo.com> - 2012-10-26 19:42 -0700
  Re: attaching names to subexpressions Gelonida N <gelonida@gmail.com> - 2012-10-27 15:03 +0200
    Re: attaching names to subexpressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-28 00:49 +0000
      Re: attaching names to subexpressions rusi <rustompmody@gmail.com> - 2012-10-27 18:57 -0700
      Re: attaching names to subexpressions Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-28 01:57 -0400
        Re: attaching names to subexpressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-28 06:59 +0000
          Re: attaching names to subexpressions Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-28 04:12 -0400
        Re: attaching names to subexpressions Neil Cerutti <neilc@norwich.edu> - 2012-10-29 11:53 +0000
      Re: attaching names to subexpressions Chris Angelico <rosuav@gmail.com> - 2012-10-28 17:01 +1100
      Re: attaching names to subexpressions "F.R." <anthra.norell@bluewin.ch> - 2012-10-28 08:12 +0100
      Re: attaching names to subexpressions Chris Angelico <rosuav@gmail.com> - 2012-10-28 21:44 +1100

csiph-web