Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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; 'languages,': 0.04; 'syntax': 0.04; 'value,': 0.04; 'elif': 0.05; 'assign': 0.07; 'context': 0.07; 'desired.': 0.07; 'expressions': 0.07; 'returned.': 0.07; 'test,': 0.07; 'assigning': 0.09; 'contexts': 0.09; 'if,': 0.09; 'present,': 0.09; 'received:212.227.126': 0.09; 'tests,': 0.09; 'try:': 0.09; '"hello': 0.16; 'assignments': 0.16; 'conditional': 0.16; 'constructs.': 0.16; 'contexts,': 0.16; 'expression,': 0.16; 'expressions.': 0.16; 'ideal.': 0.16; 'received:212.227.126.187': 0.16; 'returned,': 0.16; 'thought.': 0.16; 'typo': 0.16; 'typos': 0.16; 'all.': 0.16; 'have:': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'source': 0.25; 'possibly': 0.26; 'gets': 0.27; 'function': 0.29; 'testing': 0.29; 'wondering': 0.29; 'related': 0.29; 'errors': 0.30; 'returned': 0.30; '(which': 0.31; 'code': 0.31; 'context,': 0.31; 'equality': 0.31; 'exceptions': 0.31; 'facility': 0.31; 'indentation': 0.31; 'once,': 0.31; 'with,': 0.31; 'regular': 0.32; 'another': 0.32; 'running': 0.33; 'could': 0.34; 'common': 0.35; 'except': 0.35; 'something': 0.35; 'there': 0.35; 'false': 0.36; 'keyword': 0.36; 'useful': 0.36; 'hi,': 0.36; 'similar': 0.36; 'list.': 0.37; 'being': 0.38; 'to:addr:python- list': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'remove': 0.60; 'most': 0.60; 'john': 0.61; 'save': 0.62; 'times': 0.62; 'such': 0.63; 'more': 0.64; 'different': 0.65; 'within': 0.65; 'side': 0.67; 'risk': 0.72; "'if": 0.84; 'common,': 0.84; 'complex,': 0.84; 'world!"': 0.84; 'capture': 0.91; 'do:': 0.91; 'hand,': 0.93 Date: Thu, 02 Jan 2014 17:20:54 +0000 From: John Allsup User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Ifs and assignments Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:NVnn/chl+CYZ3PntcC7AXO3Jl5fz4kkJmw4v9T/2KcI GpoBD0/4sLY/NXvE+UGRwieaSsBMQQNkAxJxH0qrCG0tMsaffm 6ZTtWAl4YDsapwCURHndLY2a42Xb23O19azou2o0fkb/cSuFcb QZJZCJlLA6UWk+WB1RLFwTcc7t3wGjQ7dyd6y++vM+wt5+bsVh C/A9n5njPXEQGXnWRwWENYW+8+eKjL5mara/m9OpKyh3TxpTrd lSHesZ6Hlsc8Qa3h1CT29vONFUWQsuP8BleSbsYW6BCmR63s2U 0Um3AKLGAcRRRnnzyCacp+maKklp/5D/6j6fgb+8ZfjJGUjQKD vGm7UUMilwFAmhYZN21qdbYYbjq9ZDNPoApW0TWrz X-Mailman-Approved-At: Thu, 02 Jan 2014 18:26:42 +0100 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: 105 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388683604 news.xs4all.nl 2862 [2001:888:2000:d::a6]:34580 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62986 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): 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