Ternary curiousity

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

Ternary curiousity

Postby Mekire » Mon Feb 25, 2013 2:38 am

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)
but instead I get:
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.
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Ternary curiousity

Postby casevh » Mon Feb 25, 2013 5:11 am

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: 56
Joined: Sat Feb 09, 2013 7:35 am

Re: Ternary curiousity

Postby Mekire » Mon Feb 25, 2013 6:12 am

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
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Ternary curiousity

Postby ichabod801 » Mon Feb 25, 2013 1:21 pm

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


8-)
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Ternary curiousity

Postby Mekire » Mon Feb 25, 2013 2:27 pm

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

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


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 1 guest