Re: Ifs and assignments

2014-01-03 Thread Duncan Booth
Chris Angelico wrote: > Maybe a for loop isn't the best other example, but I > frequently work with places where I want to call some function and > keep iterating with the result of that until it returns false: > > while (var = func()) > { > > } > > In Python, that gets a lot clunkier.

Re: Ifs and assignments

2014-01-03 Thread Terry Reedy
On 1/2/2014 8:20 PM, Mark Lawrence wrote: On 03/01/2014 00:57, Gary Herron wrote: 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

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 3:16 PM, Ethan Furman wrote: > On 01/02/2014 04:06 PM, Chris Angelico wrote: >> >> >> Here's a crazy idea. Suppose we have a "sticky falseness" that can >> quietly propagate through an expression the way a NaN can... then we >> could just float that right through the .group(

Re: Ifs and assignments

2014-01-02 Thread Ethan Furman
On 01/02/2014 04:06 PM, Chris Angelico wrote: Here's a crazy idea. Suppose we have a "sticky falseness" that can quietly propagate through an expression the way a NaN can... then we could just float that right through the .group() call. class truth: def __new__(cls, x): if x: retu

Re: Ifs and assignments

2014-01-02 Thread Mark Lawrence
On 03/01/2014 04:25, Chris Angelico wrote: On Fri, Jan 3, 2014 at 3:14 PM, Roy Smith wrote: Or turn it into a generator: That certainly makes your mainline code cleaner... Cleaner perhaps, but not clearer. Instead of seeing the original function (say, a database retrieve-next call), you see a

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 3:14 PM, Roy Smith wrote: > Or turn it into a generator: > That certainly makes your mainline code cleaner... Cleaner perhaps, but not clearer. Instead of seeing the original function (say, a database retrieve-next call), you see a wrapper and have to go dig to find out wha

Re: Ifs and assignments

2014-01-02 Thread Roy Smith
In article , Chris Angelico wrote: > while (var = func()) > { > > } > > In Python, that gets a lot clunkier. The most popular way is to turn > it into an infinite loop: > > while True: > var = func() > if not var: break > Or turn it into a generator: def funcinator(

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 3:00 PM, Roy Smith wrote: > I do this all the time: > > t0 = time.time() > [some code] > t1 = time.time() > dt = t1 = t0 # <-- spot the typo? Yep, I see that... now that it's pointed out as a typo. Without the marker, I'd assume it's correct chained assignment, and only a

Re: Ifs and assignments

2014-01-02 Thread Roy Smith
In article , Chris Angelico wrote: > On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano > wrote: > > Personally, I find it hard to care about assignment as an expression. I find > > the obvious C-inspired syntax terrible, as it is too easy to mistakenly use > > == instead of = or visa versa: > >

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano wrote: > Personally, I find it hard to care about assignment as an expression. I find > the obvious C-inspired syntax terrible, as it is too easy to mistakenly use > == instead of = or visa versa: Python has similar problems, though. It's inherent t

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano wrote: > However, I don't think we should treat this as specific to if statements: > > for i, obj in enumerate(alist + blist + clist as items): > items[i] = process(obj) > > > Is that really better than this? > > items = alist + blist

Re: Ifs and assignments

2014-01-02 Thread Steven D'Aprano
Dennis Lee Bieber wrote: > On Thu, 02 Jan 2014 17:20:54 +, John Allsup > declaimed the following: >>In many languages, such as C, one can use assignments in conditionals >>and expressions. The most common, and useful case turns up when you > > Really? You can't do it in FORTRAN, BASIC, COB

Re: Ifs and assignments

2014-01-02 Thread Mark Lawrence
On 03/01/2014 00:57, Gary Herron wrote: 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

Re: Ifs and assignments

2014-01-02 Thread Gary Herron
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

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 10:36 AM, Roy Smith wrote: > The most common place I wish for an atomic "test and assign" is with > regexes, as in your examples. This would be so much nicer than what we > have to do now: > > if re.match(string) as m: > print m.group(0) Here's a crazy idea. Su

Re: Ifs and assignments

2014-01-02 Thread Chris Angelico
On Fri, Jan 3, 2014 at 11:06 AM, Chris Angelico wrote: > Pass any object through truth() and it'll either stay the same (if > it's true) or become this object (if it's false). You can then carry > on with other method calls, and they'll all happily return false. > > result = ( > truth(re1.matc

Re: Ifs and assignments

2014-01-02 Thread Roy Smith
In article , John Allsup wrote: > if testFunc() as x: > do something with x +1 The most common place I wish for an atomic "test and assign" is with regexes, as in your examples. This would be so much nicer than what we have to do now: if re.match(string) as m: print m.g

Re: Ifs and assignments

2014-01-02 Thread John Allsup
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

Re: Ifs and assignments

2014-01-02 Thread Tim Chase
On 2014-01-02 17:20, John Allsup wrote: > 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

Re: Ifs and assignments

2014-01-02 Thread Gary Herron
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

Re: Ifs and assignments

2014-01-02 Thread Ethan Furman
On 01/02/2014 09:20 AM, John Allsup wrote: 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.comp

Ifs and assignments

2014-01-02 Thread John Allsup
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(