Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63006
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <pydev@allsup.co> |
| 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; 'assignment': 0.07; 'class,': 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; 'inserted': 0.09; 'present,': 0.09; 'received:212.227.126': 0.09; 'tests,': 0.09; 'try:': 0.09; 'python': 0.11; '"=="': 0.16; '"hello': 0.16; 'assignments': 0.16; 'conditional': 0.16; 'constructs.': 0.16; 'contexts,': 0.16; 'expression,': 0.16; 'expressions.': 0.16; 'ideal.': 0.16; 'inconvenient': 0.16; 'received:212.227.126.171': 0.16; 'returned,': 0.16; 'thought.': 0.16; 'typo': 0.16; 'typos': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'have:': 0.19; 'appears': 0.22; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'sorry,': 0.24; 'test.': 0.24; 'source': 0.25; 'possibly': 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; 'equality': 0.31; 'exceptions': 0.31; 'facility': 0.31; 'gary': 0.31; 'indentation': 0.31; 'occurs': 0.31; 'once,': 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; 'two': 0.37; 'list.': 0.37; 'being': 0.38; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'that,': 0.38; 'ability': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'remove': 0.60; '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; '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; 'hand,': 0.93; 'comfort': 0.96 |
| Date | Thu, 02 Jan 2014 21:44:06 +0000 |
| From | John Allsup <pydev@allsup.co> |
| 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 | Re: Ifs and assignments |
| References | <52C59FF6.5000607@allsup.co> <52C5BD90.9020609@islandtraining.com> |
| In-Reply-To | <52C5BD90.9020609@islandtraining.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:i3+XIKcL/FFH5Nry7c07rA4WXuFp0ZupQUFc1c8Qtx3 pKuVy6CqwdQeT0Ab1Uv8V1wHp43kWStRuSMAfO+BznO8OqqE3K 4Al8WdrG/krx3PvopdC+pZ/GU9x6o0He2THUJVPpKB5RV6U91e Mt6dQzzwhsDEvZ2yaSmZ8hFRIzoGbHjCjP0TBXjTcfzzrb4i6f 3pUSPRLo7VnxxXs8q80tOKvp1+WzwtQP15G/7ZqeXdvtZ/bjuY WNMJtDtg2vNWpREms+tdmIzaRLAQrbkR0s4UqHOJ5TJQJHSIWD 0LU8o3OB2ag6o0ZzJQDDT9p5FHLO90rTWyZmIqH6URn17e0OfQ deeEt0n5d+/1q3pG7SSWpe/BOzhNvMuyGdgDteL3w |
| X-Mailman-Approved-At | Fri, 03 Jan 2014 00:13:39 +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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4811.1388704420.18130.python-list@python.org> (permalink) |
| Lines | 148 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1388704420 news.xs4all.nl 2845 [2001:888:2000:d::a6]:59399 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:63006 |
Show key headers only | View raw
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
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
>>
>
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Ifs and assignments John Allsup <pydev@allsup.co> - 2014-01-02 21:44 +0000
Re: Ifs and assignments Roy Smith <roy@panix.com> - 2014-01-02 18:36 -0500
Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 11:07 +1100
Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 11:06 +1100
Re: Ifs and assignments Ethan Furman <ethan@stoneleaf.us> - 2014-01-02 20:16 -0800
Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 16:35 +1100
csiph-web