## Ternary curiousity

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

### Ternary curiousity

I just came across something interesting; or rather strange.
I wrote a ternary statement in which a was trying to assign one tuple or another tuple based on a condition.

For example:
Code: Select all
`for i in range(5):    this = 4,5 if i%2 else 6,7    print(this)`
My expected result was:
Code: Select all
`(6, 7)(4, 5)(6, 7)(4, 5)(6, 7)`
Code: Select all
`(4, 6, 7)(4, 5, 7)(4, 6, 7)(4, 5, 7)(4, 6, 7)`

If I put parenthesis around the tuples I get the expected result as in:
Code: Select all
`for i in range(5):    this = (4,5) if i%2 else (6,7)    print(this)`

Syntactically I don't know why they are interpreted differently. Anyone know exactly why this is occurring?
-Mek

Edit: Never mind I figured out what the interpreter was thinking. It interpreted my statement as the tuple (4,(5 if i%2 else 6),7). I didn't realize the ternary syntax was quite this flexible before. Apparently you can put a ternary conditional anywhere you would assign a value normally. I thought the syntax was much stricter and restricted to basic assignment. Things like this just don't look like they should be allowed:
Code: Select all
`for i in range(5):    this = 4 if i%2 else 5,6 if i%3 else 7,8 if i%4 else 9    print(this)`
It has opened my eyes to a new world of convoluted incomprehensible code.
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Ternary curiousity

The key is that "x if p else y" is a conditional expression, just like "x+y". It is not a statement like "if ... else ...".

I'm not really a fan of convoluted one-liners. But if you really want to do this:

for i in range(5):
this = 4 if i%2 else 5,6 if i%3 else 7,8 if i%4 else 9
print(this)

at least write it like this:

for i in range(5):
this = ((4 if i%2 else 5), (6 if i%3 else 7), (8 if i%4 else 9))
print(this)

I have worked with programming languages that evaluated operators from left-to-right without any precedence rules. I've developed the habit of using extra parenthesis to eliminate any ambiguity about how an expression should be evaluated.
casevh

Posts: 114
Joined: Sat Feb 09, 2013 7:35 am

### Re: Ternary curiousity

Oh, I don't want to do it. I just didn't realize I could before. As you said, I didn't realize that these conditionals were just normal expressions that could be placed anywhere. I have used ternaries quite frequently but only ever one per line so I had inferred that this was a requirement (and it might be better if it was).

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Ternary curiousity

If you had used my ternary syntax that would have been clearer:

Code: Select all
`for i in range(5):   this = [(6, 7), (4, 5)][i % 2]   print this`

Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801

Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

### Re: Ternary curiousity

Haha. I was experimenting with that just for kicks. But the i%2 index is far to obvious; not nearly obfuscated enough.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona