Path: csiph.com!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python.': 0.02; 'else:': 0.03; 'languages,': 0.04; 'syntax': 0.04; 'value,': 0.04; 'elif': 0.05; 'assign': 0.07; 'assignment': 0.07; 'class,': 0.07; 'context': 0.07; 'desired.': 0.07; 'expressions': 0.07; 'nicely': 0.07; 'returned.': 0.07; 'test,': 0.07; 'assigning': 0.09; 'consistency': 0.09; 'contexts': 0.09; 'if,': 0.09; 'inserted': 0.09; 'present,': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'tests,': 0.09; 'try:': 0.09; 'python': 0.11; '"=="': 0.16; '"hello': 0.16; 'assignments': 0.16; 'c/c++': 0.16; 'conditional': 0.16; 'constructs.': 0.16; 'contexts,': 0.16; 'expression,': 0.16; 'expressions.': 0.16; 'ideal.': 0.16; 'inconvenient': 0.16; 'returned,': 0.16; 'suggestion.': 0.16; 'thought.': 0.16; 'typo': 0.16; 'typos': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'have:': 0.19; '>>>': 0.22; 'appears': 0.22; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'received:emailsrvr.com': 0.24; 'sorry,': 0.24; 'test.': 0.24; 'source': 0.25; 'possibly': 0.26; 'received:(smtp server)': 0.26; 'suggested': 0.26; 'post': 0.26; 'least': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'testing': 0.29; 'wondering': 0.29; 'am,': 0.29; 'character': 0.29; 'related': 0.29; 'errors': 0.30; 'returned': 0.30; '(which': 0.31; 'code': 0.31; 'lines': 0.31; 'context,': 0.31; 'end,': 0.31; 'equality': 0.31; 'exceptions': 0.31; 'facility': 0.31; 'gary': 0.31; 'indentation': 0.31; 'occurs': 0.31; 'once,': 0.31; 'post.': 0.31; 'with,': 0.31; 'workaround': 0.31; 'regular': 0.32; 'another': 0.32; 'running': 0.33; 'style': 0.33; 'could': 0.34; 'common': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'keyword': 0.36; 'whilst': 0.36; 'useful': 0.36; 'hi,': 0.36; 'similar': 0.36; 'example,': 0.37; 'two': 0.37; 'list.': 0.37; 'being': 0.38; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'rather': 0.38; 'that,': 0.38; 'ability': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'easy': 0.60; 'is.': 0.60; 'most': 0.60; 'john': 0.61; 'further': 0.61; 'save': 0.62; 'times': 0.62; 'kind': 0.63; 'such': 0.63; 'more': 0.64; 'different': 0.65; 'within': 0.65; 'here': 0.66; 'side': 0.67; 'between': 0.67; 'advantages': 0.68; 'risk': 0.72; 'apart': 0.72; "'if": 0.84; "'with'": 0.84; 'avoids': 0.84; 'clearer': 0.84; 'common,': 0.84; 'complex,': 0.84; 'confusion.': 0.84; 'convinced,': 0.84; 'replicate': 0.84; 'world!"': 0.84; 'capture': 0.91; 'do:': 0.91; 'sorry.': 0.91; 'hand,': 0.93; 'comfort': 0.96 X-Virus-Scanned: OK Date: Thu, 02 Jan 2014 16:57:26 -0800 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Ifs and assignments References: <52C59FF6.5000607@allsup.co> <52C5BD90.9020609@islandtraining.com> <52C5DDA6.5090207@allsup.co> In-Reply-To: <52C5DDA6.5090207@allsup.co> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 163 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388711203 news.xs4all.nl 2892 [2001:888:2000:d::a6]:36540 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63015 On 01/02/2014 01:44 PM, John Allsup wrote: > The point of my original post was that, whilst C's > if( x = 2 ) { do something } > and > if( x == 2 ) { do something } > are easy to confuse, and a source of bugs, having a construct like > follows: > > if x == 2: > do something # what happens at present > if testFunc() as x: > do something with x > > using the 'as' syntax that appears with 'with' and 'except', would allow > for the advantages of C style assignments in conditionals but without > the easy confusion, since here the syntax is significantly different > between assignment and equality testing (rather than a character apart > as happens with C). > > This occurs further down in my original post (past the point where you > inserted your reply). > > Another post suggested a workaround by defining a 'pocket' class, for > which I am grateful. > > John Sorry. I shot off my answer before reading the whole post. That's never a good idea. After reading to the end, I rather like your suggestion. It works well in your example, , nicely avoids the C/C++ trap, and has some consistency with other parts of Python. Gary Herron > > > On 02/01/2014 19:27, Gary Herron wrote: >> On 01/02/2014 09:20 AM, John Allsup wrote: >>> Hi, >>> >>> This is my debut on this list. >>> >>> In many languages, such as C, one can use assignments in conditionals >>> and expressions. The most common, and useful case turns up when you >>> have if/else if/else if/else constructs. Consider the following >>> non-working pseudoPython. >>> >>> import re >>> r1 = re.compile("hello (\d)") >>> r2 = re.compile("world([!?])") >>> >>> w = "hello world!" >>> >>> if m = r1.search(w): >> >> This kind of thing in C/C+ has always been the source of much confusion >> and potential errors, because the construct is so similar to an "==" >> test. Python does not replicate this potential for confusion. Instead, >> we use two lines of code, an assignment and then the test. If you find >> that extra line of code inconvenient than at least you can take comfort >> in the fact that it is clearer code. If you are still not convinced, >> ... then sorry, that's just the way Python is. >> >> Gary Herron >> >> >>> handleMatch1(m) >>> elif m = r2.search(w): >>> handleMatch2(m) >>> else: >>> print("No match") >>> >>> If the regular expressions are complex, running them multiple times >>> (once to test, another to capture groups) isn't ideal. On the other >>> hand, at present, one has to either do: >>> >>> m = r1.search(w) >>> if m: >>> handleMatch1(m) >>> else: >>> m = r2.search(w) >>> if m: >>> handleMatch2(m) >>> else: >>> print("No match") >>> >>> if not running unnecessary matches, yet capturing groups in the event >>> of a successful match, is what is desired. >>> >>> If there are multiple tests, the indentation gets silly. This arises >>> because having removed the ability to assign in an expression, there >>> is no way to save the result of a function call that is used in a >>> conditional at all. >>> >>> I am aware that this facility in C is a source of bugs, = being only a >>> typo away from the more common ==. With exceptions and contexts, we >>> have: >>> >>> with open("file") as f: >>> doSomethingWith(f) >>> >>> try: >>> trySomething() >>> except SomethingRandomGoingWrong as e: >>> lookAtException(e) >>> >>> What I am wondering is why not use a similar syntax with if, so that >>> one could do >>> >>> if r1.search(w) as m: >>> g = m.groups() >>> print(g[1]) >>> >>> This would remove the risk of errors by typos since the syntax for >>> equality testing (if x == y:) is completely different from that for >>> assigning in a conditional (which would look like 'if y as x:' >>> >>> Related would be to have Nonetype work with contexts such that >>> >>> with None as x: >>> doStuff(x) >>> >>> would do nothing. This would allow things like: >>> >>> with maybeGetSomething as x: >>> doStuff(x) >>> >>> to call doStuff(x) within a context of maybeGetSomething returns >>> something, or do nothing if nothing is returned. (Adding an else-like >>> keyword to with, or possibly using else in that context, would allow >>> one to process a non-None object if returned, or else do something in >>> response to a None object being returned by the maybeGetSomething.) >>> >>> Just a thought. >>> >>> Or what is the current 'Pythonic' way to do something like: >>> >>> if x = func1(): >>> do1(x) >>> elif x = func2(): >>> do2(x) >>> elif x = func3(): >>> do3(x) >>> elif x = func4(): >>> do4(x) >>> else: >>> do5() >>> >>> where each of func1,func2,func3,func4 have side effects so that func2 >>> is tested if and only if func1 returns a false value, func1 must be >>> called only once, and what is returned from func1 must be available to >>> the code inside the if block? >>> >>> >>> John >>> >> >