ternary operator

This is the place for queries that don't fit in any of the other categories.

ternary operator

Postby pajtonak » Sun Dec 01, 2013 5:28 pm

Hello everyone,

can someone explain me please, why following ternary operator statement:

Code: Select all
val = 1
val += 1 if val == 1 else val -= 1


throws an error:
Code: Select all
  File "ternary.py", line 4
    val += 1 if val == 1 else val -= 1
                                   ^
SyntaxError: invalid syntax

?

The code works when i use full multiline if else statement..

Thanks in advance for any hint,

Best regards,
P.
Last edited by stranac on Sun Dec 01, 2013 6:18 pm, edited 1 time in total.
Reason: First post lock.
pajtonak
 
Posts: 3
Joined: Sun Dec 01, 2013 5:21 pm

Re: ternary operator

Postby stranac » Sun Dec 01, 2013 6:24 pm

Well, you can't have a statement(such as assignment) inside a ternary expression.

Basically,
Code: Select all
a if condition else b
returns a if condition is true, otherwise it returns b.
Since val -= 1 isn't something that can be returned, your code is invalid.

The fix is simple enough:
Code: Select all
val += 1 if val == 1 else -1

This adds to val 1 if val == 1, otherwise it adds -1.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: ternary operator

Postby Mekire » Mon Dec 02, 2013 12:14 am

I wrote a brief tutorial on this some time ago.
You can find it here:
http://python-forum.org/viewtopic.php?f=25&t=320

Also, as I mention there, I think adding parenthesis can greatly increase readability.
Code: Select all
val += (1 if val==1 else -1)

-Mek
User avatar
Mekire
 
Posts: 1140
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: ternary operator

Postby micseydel » Mon Dec 02, 2013 2:39 am

I really detest extra parenthesis. I feel like they're excessive here. I would use them for a complex subexpression in a ternary expression but not just wrapping the whole thing.

Also, for the particular version here, I would just use a regular if structure because I find that more readable than trying to add -1 to something, although it really isn't that bad.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ternary operator

Postby pajtonak » Mon Dec 02, 2013 6:45 am

hello, thanks all for your inputs.
however, both
Code: Select all
val = 1
val += (1 if val == 1 else -1)
print "value: " val


&

Code: Select all
val = 1
val += 1 if val == 1 else -1
print "value: " val


produces:
Code: Select all
    print "value: " val
                      ^
SyntaxError: invalid syntax


so none of your solutions works...

anyway, i appreciate your effort to help!
pajtonak
 
Posts: 3
Joined: Sun Dec 01, 2013 5:21 pm

Re: ternary operator

Postby micseydel » Mon Dec 02, 2013 7:40 am

Every solution works, it gets you past that line. You didn't show us the next line, which is incorrect for another reason. You put a string and a variable next each other without any description of the relation you want between them (plus, minus, multiplication, whatever).

Here's what I recommend doing
Code: Select all
print "value: {}".format(val)

You'll need to put a 0 in between the curly braces there if you're using less than Python 2.7.

You can also do this, which newbies tend to do and is fine for one-offs playing around in the interpreter but I recommend against making it a habit
Code: Select all
print "value: " + str(val)

I also just realized the following is quite acceptable
Code: Select all
print "value", val
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ternary operator

Postby pajtonak » Mon Dec 02, 2013 9:33 am

omg, sorry... it was early in the morning i was replying, i completely overlooked that the issue occured on the print line... :oops: :oops: :oops: :oops:

thanks again, issue solved!
pajtonak
 
Posts: 3
Joined: Sun Dec 01, 2013 5:21 pm

Re: ternary operator

Postby Kebap » Mon Dec 02, 2013 12:09 pm

micseydel wrote:I also just realized the following is quite acceptable
Code: Select all
print "value", val

I use this all the time and am really going to miss it in python 3.

As to the original question, I find ternary operations utterly unreadable and might have made the same mistake as the OP, so wrapping the whole thing in () would have improved readability for me a lot. Still the best to me though:

Code: Select all
If val == 1:
    val += 1
else:
    val -= 1
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 403
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: ternary operator

Postby stranac » Mon Dec 02, 2013 1:04 pm

Kebap wrote:I use this all the time and am really going to miss it in python 3.

You can do that just fine in python 3(although you need parens, since it's a function).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Keepsake, snippsat and 4 guests