Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: for / while else doesn't make sense Date: Mon, 6 Jun 2016 22:35:33 -0600 Lines: 76 Message-ID: References: <7d453116-c02a-4211-836e-518f571c29a3@googlegroups.com> <4aafb237-4c41-4c0f-98ec-f71841c36bc1@googlegroups.com> <48de4319-7c8a-419a-978c-3cff46f3490a@googlegroups.com> <57524448$0$1615$c3e8da3$5496439d@news.astraweb.com> <12226874-a705-47b1-859c-e9bed59ac6bb@googlegroups.com> <6c3b3d2e-bfc0-4c0e-ab81-55ec672f06c9@googlegroups.com> <7eb86c69-e0c5-4a4e-b35e-d8404bbebf9f@googlegroups.com> <878tyj97zo.fsf@elektro.pacujo.net> <6fde6944-918a-443e-b44f-0c7a69604a44@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de lwWfscWyoMYiukTY/1OT3gX56YZSBki7yU8/wYVyfFRA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'context': 0.05; '<>,': 0.07; 'exit': 0.07; 'none)': 0.07; 'global,': 0.09; 'subject:while': 0.09; 'url:github': 0.09; 'received:209.85.218': 0.10; 'def': 0.13; 'variables': 0.15; '2016': 0.16; 'constructs': 0.16; 'function?': 0.16; 'iterator,': 0.16; 'iterators': 0.16; 'looping': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'return,': 0.16; 'scopes': 0.16; 'stepping': 0.16; 'subject:make': 0.16; 'tempted': 0.16; 'true:': 0.16; 'url:py': 0.16; 'versatile': 0.16; 'wrote:': 0.16; 'string': 0.17; 'nested': 0.18; 'try:': 0.18; 'input': 0.18; 'suggested': 0.20; 'lawrence': 0.22; 'simpler': 0.22; 'appears': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'sort': 0.25; 'example': 0.26; 'figure': 0.27; 'message-id:@mail.gmail.com': 0.27; 'processed': 0.27; 'yield': 0.27; 'regular': 0.29; 'it\xe2\x80\x99s': 0.29; 'comments': 0.30; 'code': 0.30; "i'd": 0.31; 'another': 0.32; 'class': 0.33; 'problem': 0.33; 'hell': 0.33; 'suit': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'replace': 0.35; 'quite': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'needed': 0.36; 'possible.': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'seem': 0.37; 'things': 0.38; 'received:209': 0.38; 'end': 0.39; 'why': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'close': 0.61; 'avoid': 0.61; 'skip:n 10': 0.62; 'above,': 0.63; 'subject: / ': 0.63; 'different': 0.63; 'goal': 0.64; 'rare': 0.66; 'special': 0.73; 'common,': 0.84; 'complex.': 0.84; 'experiment': 0.84; 'hand.': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; 'to:name:python': 0.84; 'url:master': 0.84; 'visually': 0.84; 'yikes,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-transfer-encoding; bh=g79sJgPoaaon315FafaaW9X2moT+7zFUeeX41YT5zLs=; b=tMrshNXbaLntDiZKiZm4O2Dcb7YBThqPNicMRr6KZ/M0ugI4b49RCpOTRG7N5bwPI5 gm2qGq6/wQ0BdzN4DjqbCX+MjuW3Ca5NQIblD20oU65Yel4HA9hZFksV4HlCWf1HtFwJ mnc1ee/ezOsVbS/iVBqMU6dWLaITLopSFIJNy+O6z8T3h7wXjvtk94JFpcSlD65boAA4 OuyETgfCacW5V37ACM0hCt3UNfcDs1kAEsFiocl7wvV9FK2jf5uedGyTsM7YOvPOouxD VBGCuZMtuUgfIBb9pyvKHugOVEn589erCWoSFj8vIHw0uGSXaoX6YKbIPWGtMdtdHEK2 sKFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-transfer-encoding; bh=g79sJgPoaaon315FafaaW9X2moT+7zFUeeX41YT5zLs=; b=VgIiFezJXQtrZ2VNZPWWmQT3nElvm8UjaGraa8We7PIGTJn3pKCwQU/vIgMj1WxNwz TDzW6iZqm3AkC8pOB0SVgIG4m286ePGKhQ6iUl+yiMgCt14QHzO5H3Ht+RMIsok0LYX0 7Opf4nyfJuyMNAXigu3h7/mngFJe0UDyNbJTTlSQrj9zQ5th/WEtrylDf7l11312mPxS rOW9l4YUd8InGFKqYuxy9chua8zYKbCvvQZQt4jdZiMNI+Du+aaCoJaG+1y7xpew/QdT GbGxdOqcndZpg009irYg0ZOhaAPr6TKEfLWkCunNm2HBuPp0qjyPKX0ZiLhvgLzxYrZl Xsuw== X-Gm-Message-State: ALyK8tIiBdZtiNPMCqe90T0Gf32dElzNRfW/5a7qsMwjW/MCGDL1snTeBQv39FMMhgafqlnx1FgJPTViymia7A== X-Received: by 10.202.51.133 with SMTP id z127mr10428666oiz.194.1465274173200; Mon, 06 Jun 2016 21:36:13 -0700 (PDT) In-Reply-To: <6fde6944-918a-443e-b44f-0c7a69604a44@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <7d453116-c02a-4211-836e-518f571c29a3@googlegroups.com> <4aafb237-4c41-4c0f-98ec-f71841c36bc1@googlegroups.com> <48de4319-7c8a-419a-978c-3cff46f3490a@googlegroups.com> <57524448$0$1615$c3e8da3$5496439d@news.astraweb.com> <12226874-a705-47b1-859c-e9bed59ac6bb@googlegroups.com> <6c3b3d2e-bfc0-4c0e-ab81-55ec672f06c9@googlegroups.com> <7eb86c69-e0c5-4a4e-b35e-d8404bbebf9f@googlegroups.com> <878tyj97zo.fsf@elektro.pacujo.net> <6fde6944-918a-443e-b44f-0c7a69604a44@googlegroups.com> Xref: csiph.com comp.lang.python:109603 On Mon, Jun 6, 2016 at 6:51 PM, Lawrence D=E2=80=99Oliveiro wrote: > On Sunday, June 5, 2016 at 11:43:20 PM UTC+12, Marko Rauhamaa wrote: >> I often experiment with different loop constructs to find the one most >> pleasing to the eye. Working directly off iterators is quite rare but a >> while-vs-for consideration is frequent. Also, should the stepping part >> be in the beginning, middle or end of the loop body? > > It is nice to have a common, versatile looping form which can be arranged= in different ways to suit the problem at hand. That=E2=80=99s why I like t= he C-style for-statement. > > Here=E2=80=99s another example , for the consideration of those who don=E2=80=99t seem = too familiar with looping: 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. It took me a lot of scrolling up and down just to figure out what the scopes of the variables were. > while True : > while True : > line =3D next(input_line, None) > if line !=3D None : > if len(include_stack) =3D=3D 0 : > linenr +=3D 1 > #end if > break > #end if > if len(include_stack) =3D=3D 0 : > break > input_line =3D include_stack.pop() > #end while > if line =3D=3D None or ... line contains something special ... : > ... > if line =3D=3D None : > break > ... process special line ... > ... replace with None to indicate it=E2=80=99s been processed= ... > #end if > if line !=3D None : > ... process regular line ... > #end if > #end while def generate_lines(): nonlocal input_line while True: try: # Note input_line appears to be an iterator, not a string # as suggested by the name. yield next(input_line) except StopIteration: if include_stack: input_line =3D include_stack.pop() else: return for line in generate_lines(): if not include_stack: linenr +=3D 1 if ... line contains something special ...: ... process special line ... else: ... process regular line ... Much simpler than the nested while loop hell above, and not a single break needed (if you don't count the return, that is; in any case each loop has a single exit point). I'd be tempted to refactor input_line and include_stack into some sort of input context class and make generate_lines a method of the class in order to avoid having those variables be (effectively) global, but my goal was to keep this as close to the original in design as possible.