Re: If One Line

2014-12-25 Thread alex23
On 26/12/2014 1:37 PM, Steven D'Aprano wrote: One approach is to use a function in the condition to do the assignment: Let me fix that for you: /s/approach/bad idea/ I never said it was a _good_ approach ;) And you don't even save any lines! Instead of a one-liner, you have six lines! Wh

Re: If One Line

2014-12-25 Thread Steven D'Aprano
alex23 wrote: > On 26/12/2014 1:18 AM, JC wrote: >> Is it possible in python: >> >> if ((x = a(b,c)) == 'TRUE'): >> print x > > One approach is to use a function in the condition to do the assignment: Let me fix that for you: /s/approach/bad idea/ All you have done is replace one anti-pattern

Re: If One Line

2014-12-25 Thread Steven D'Aprano
JC wrote: > Hello, > > Is it possible in python: > > if ((x = a(b,c)) == 'TRUE'): > print x Fortunately, no. Assignment as an expression is an anti-pattern and a bug magnet. The above is best written as: if a(b,c) == 'TRUE': print 'TRUE' If you need the result of calling the a() fun

Re: If One Line

2014-12-25 Thread alex23
On 26/12/2014 1:18 AM, JC wrote: Is it possible in python: if ((x = a(b,c)) == 'TRUE'): print x One approach is to use a function in the condition to do the assignment: x = None def assign_to_x(val): global x x = val return val def a(x, y):

Re: If One Line

2014-12-25 Thread Jacob Kruger
ro To: Jacob Kruger Cc: Python Sent: Thursday, December 25, 2014 7:45 PM Subject: Re: If One Line > Actually more that in the interpreter, it's prompting me with ... as if I had left out a closing ) or something, but, suppose it could work fine in an actual imported bit of c

Re: If One Line

2014-12-25 Thread Jacob Kruger
ro To: Jacob Kruger Cc: Python Sent: Thursday, December 25, 2014 7:45 PM Subject: Re: If One Line > Actually more that in the interpreter, it's prompting me with ... as if I had left out a closing ) or something, but, suppose it could work fine in an actual imported bit of c

Re: If One Line

2014-12-25 Thread Skip Montanaro
> Actually more that in the interpreter, it's prompting me with ... as if I had left out a closing ) or something, but, suppose it could work fine in an actual imported bit of code? That's how it's supposed to work. Given that Python block structure is determined by indentation, you need some way

Re: If One Line

2014-12-25 Thread Jacob Kruger
janitor's closet..." - Original Message - From: Skip Montanaro To: Jacob Kruger Cc: Python Sent: Thursday, December 25, 2014 6:26 PM Subject: Re: If One Line I don't get an error. >>> I = 1 if True else 2 >>> if I == 1: print("one&

Re: If One Line

2014-12-25 Thread Rick Johnson
On Thursday, December 25, 2014 9:19:25 AM UTC-6, JC wrote: > Hello, > > Is it possible in python: > > if ((x = a(b,c)) == 'TRUE'): > print x > > Thanks. Could you not simply rephrase: result = foo() if result == 'TRUE': do_something() Of course, another oddity is fact th

Re: If One Line

2014-12-25 Thread Rick Johnson
On Thursday, December 25, 2014 10:16:54 AM UTC-6, Jacob Kruger wrote: > One line assignment is ok, but, seems like you can't perform actions. > > #the following will work: > I = 1 if True else 2 > > #but the following will generate an error: > if I == 1: print("one") Only if "I" is undefined. --

Re: If One Line

2014-12-25 Thread Skip Montanaro
I don't get an error. >>> I = 1 if True else 2 >>> if I == 1: print("one") ... one >>> What error did you get? Skip -- https://mail.python.org/mailman/listinfo/python-list

Re: If One Line

2014-12-25 Thread Jacob Kruger
ind Biker Skype: BlindZA "Roger Wilco wants to welcome you...to the space janitor's closet..." - Original Message - From: "JC" Newsgroups: comp.lang.python To: Sent: Thursday, December 25, 2014 5:18 PM Subject: If One Line Hello, Is it possible in python: if (

Re: If One Line

2014-12-25 Thread Jacob Kruger
ind Biker Skype: BlindZA "Roger Wilco wants to welcome you...to the space janitor's closet..." - Original Message - From: "JC" Newsgroups: comp.lang.python To: Sent: Thursday, December 25, 2014 5:18 PM Subject: If One Line Hello, Is it possible in python: if (

Re: If One Line

2014-12-25 Thread Ian Kelly
On Thu, Dec 25, 2014 at 8:18 AM, JC wrote: > > Hello, > > Is it possible in python: > > if ((x = a(b,c)) == 'TRUE'): > print x No, assignments in Python are statements, not expressions. -- https://mail.python.org/mailman/listinfo/python-list

Re: If One Line

2014-12-25 Thread Fetchinson .
> Is it possible in python: > > if ((x = a(b,c)) == 'TRUE'): > print x Nope. Assignment is not allowed in a conditional. Cheers, Daniel > Thanks. > -- > https://mail.python.org/mailman/listinfo/python-list > -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- https://m

If One Line

2014-12-25 Thread JC
Hello, Is it possible in python: if ((x = a(b,c)) == 'TRUE'): print x Thanks. -- https://mail.python.org/mailman/listinfo/python-list