Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'syntax': 0.03; 'convention.': 0.07; 'data:': 0.07; 'granted,': 0.07; '"if': 0.09; 'exits': 0.09; 'similar,': 0.09; 'subject:while': 0.09; 'cc:addr:python-list': 0.10; 'cases': 0.15; '"m"': 0.16; '"while': 0.16; '"while"': 0.16; '"with"': 0.16; '(usually)': 0.16; '-tkc': 0.16; 'accordingly': 0.16; 'both)': 0.16; 'cascading': 0.16; 'expr': 0.16; 'foo"': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterating': 0.16; 'loops': 0.16; 'merely': 0.16; 'message- id:@tim.thechases.com': 0.16; 'readable': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'regex,': 0.16; 'simpson': 0.16; 'statement.': 0.16; 'subject:expression': 0.16; 'ternary': 0.16; 'those,': 0.16; 'true:': 0.16; 'ugly.': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'replacing': 0.17; 'tim': 0.18; 'proposed': 0.20; 'putting': 0.20; 'written': 0.20; 'bit': 0.21; 'assignment': 0.22; 'context.': 0.22; 'exists.': 0.22; 'parse': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'nearly': 0.23; 'this:': 0.23; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '(which': 0.26; 'common': 0.26; "doesn't": 0.28; 'bad.': 0.29; 'chase': 0.29; 'statements': 0.29; 'yields': 0.29; 'this.': 0.29; "i'm": 0.29; 'usually': 0.30; 'becomes': 0.30; 'function': 0.30; 'stuff': 0.30; 'expect': 0.31; 'sense': 0.31; 'code': 0.31; 'could': 0.32; "aren't": 0.33; 'true.': 0.33; 'handle': 0.33; 'languages': 0.33; 'done': 0.34; 'list': 0.35; 'decisions': 0.35; 'something': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'functional': 0.36; 'test': 0.36; 'does': 0.37; 'being': 0.37; 'quite': 0.37; 'far': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'some': 0.38; 'possible.': 0.38; 'sure': 0.38; 'instead': 0.39; 'where': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'more': 0.63; 'natural': 0.65; 'results': 0.65; 'as:': 0.75; 'construct': 0.84; 'doable': 0.84; 'idiomatic': 0.84; 'improvement': 0.84; 'parity': 0.84; 'received:50.22': 0.84; 'stop,': 0.84; 'worthwhile.': 0.84 Date: Wed, 24 Oct 2012 17:54:33 -0500 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Cameron Simpson Subject: Re: while expression feature proposal References: <50886398.5050301@tim.thechases.com> <20121024222656.GA32708@cskk.homeip.net> In-Reply-To: <20121024222656.GA32708@cskk.homeip.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 144 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351119215 news.xs4all.nl 6968 [2001:888:2000:d::a6]:56879 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32075 On 10/24/12 17:26, Cameron Simpson wrote: > On 24Oct2012 16:54, Tim Chase wrote: > | On 10/24/12 16:34, Ian Kelly wrote: > | > The idiomatic way to do this is: > | > > | > while True: > | > VAR = EXPR > | > if not VAR: > | > break > | > BLOCK > | > | It may be idiomatic, but that doesn't stop it from being pretty > | ugly. > > Yes, but more flexible because it accomodates loops where the natural > place for the test is partway through the loop instead of right at the > top, which is quite common. Just like the "with" doesn't stop you from using a try/finally block to do something similar, you can still write mid-loop exits as code currently exists. The proposed syntax just makes coming from other languages easier to translate. > | I must say I really like the parity of Dan's > | while EXPR as VAR: > | BLOCK > | proposal with the "with" statement. > > Well, it's nice. But usually EXPR will be a boolean. I think the most common use-cases are boolean'ish. The falsehood means stop, but the truthiness value is not merely True. The common use-case I hit is the f = file('foo.bin') while True: data = f.read(CHUNK_SIZE) if not data: break process(data) #not just data=True which tidily becomes while f.read(CHUNK_SIZE) as data: process(data) > m = re_FUNKYPATTERN.match(test_string) > if m: > do stuff with the results of the match, using "m" > > If I could write this as: > > if re_FUNKYPATTERN.match(test_string) as m: > do stuff with the results of the match, using "m" I'm -0 on this. I don't like the pattern, but I'm not sure the "if CONDITION as FOO" syntax yields nearly the functional & readable improvement of "while THING as FOO". > then some cascading parse decisions would feel a bit cleaner. Where I > current have this: > > m = re_CONSTRUCT1.match(line) > if m: > ... handle construct 1 ... > else: > m = re_CONSTRUCT2.match(line) > if m: > ... handle construct 2 ... > else: > m = re_CONSTRUCT3.match(line) For those, I often end up putting the regexps in a list and iterating accordingly (which I have done multiple times): regexes = [ re_one, re_two, re_three, ] for regex in regexes: m = regex.match(line) if m: handle(m) break else: doh() Granted, it does expect that you want to handle the results uniformly, but this could (usually) be mitigated with a dispatch function as well regex_dispatches = [ (re_one, handle1), (re_two, handle2), (re_three, handle3), ] for regex, fn in regex_dispatches: m = regex.match(line): if m: fn(m, extra_context) break else: doh() > But I'm still -0 on it, because it supplants the glaringly obvious: > > m = ... > > assignment with the far less in your face: > > possibly-long-expr as m If it were replacing standard assignment, I'd be as off-the-charts -1 as possible. Yech. But when cued by the "while" and indentation, it's not quite so bad. It still feels yuckyâ„¢ in an "if", but I find the improvement in the "while" is certainly worthwhile. > With statements and except statements have concrete use cases for the > "as" part that aren't doable without it, but the while/if...as form > can always be written in the current convention. The syntax for the with *could* have been something like with foo = file("foo.txt"): for line in foo: pass but the "as" was chosen and I think it makes sense in the context. -tkc (To me, the "if CONDITION as VAR" feels much like the ternary evaluation: x = foo if bar else baz which made it into official syntax; I find the "while CONDITION as VAR" much more readable/valuable in light of both)