[MOVED] Ternary/Conditional Expressions

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

[MOVED] Ternary/Conditional Expressions

Postby Mekire » Thu Feb 21, 2013 11:10 am

Last edited by Mekire on Thu Feb 21, 2013 11:39 pm, edited 1 time in total.
User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Ternary/Conditional Expressions

Postby metulburr » Thu Feb 21, 2013 1:41 pm

Apparently some Python programmers seem to think it should be avoided

I am one of these programmers. One reason is when i scan code, and often miss an else/elif clause. I find the standard more structured, and noticeable. Now i like how python makes things condensed compared to other languages. But in some cases that feature turns into hinderance. This is also the reason i tend to stay away from list comps. I find it looks more cluttered. Even though its a little faster, i like the more readable standard approach. But thats just my option. Good post though!
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Ternary/Conditional Expressions

Postby Mekire » Thu Feb 21, 2013 2:15 pm

Haha. Thankyou. I also love comprehensions. I think the time I spent doing some of the problems on Project Euler ruined me though. I like to try to turn complex algorithms into one-liners whenever possible. I will be the first to admit that at its worst this certainly doesn't aid readability and is far from Pythonic.

One thing I forgot to add. I find that placing parenthesis like so:
Code: Select all
a += (1 if b%2 else 2)
can massively improve the readability of such conditional statements (especially those with augmented assignment ops).

-Mek
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Ternary/Conditional Expressions

Postby ichabod801 » Thu Feb 21, 2013 2:56 pm

A similar thing I sometimes do is use a boolean as an index for a len == 2 list. An example from this morning's bus ride:

Code: Select all
det = ['a', 'an'][words[1][0] in 'aeiouh']


instead of:

Code: Select all
if words[1][0] in 'aeiouh':
   det = 'an'
else:
   det = 'a'


Again, it is more concise but it hides the conditional that would otherwise be there.
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/Conditional Expressions

Postby micseydel » Thu Feb 21, 2013 6:03 pm

I do use the ternary operator, although I just looked at my code to give an example and when I was reading it I realized the code was incorrect... so I certainly can't defend it!

ichabod that's terrible! (I've done it before too, but I learned better! Especially since there's a ternary operator now, if you really wanted to do that).

Mekire, in your original post, I find the spacing (or lack thereof) in the first ternary example to be very confusing, as it implies that the += occurs or 2 does, rather than the += being applied to the whole right hand side.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 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.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Ternary/Conditional Expressions

Postby ichabod801 » Thu Feb 21, 2013 7:14 pm

micseydel wrote:ichabod that's terrible!


8-)

It just seems that choosing between two values for a single variable is a waste of a good conditional.
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/Conditional Expressions

Postby Somelauw » Sat Feb 23, 2013 12:17 am

The thing i don't like about ternaries in python is that it reads kinda backwards. I do like ternaries in other languages that use the ?:-syntax.

Lisp and scala do something neat because in those languages there is no difference between an if statements and an if-ternary.
You can just write:
Code: Select all
(let a (if (zero? (rem b 2)) 2 1) ...)

or
Code: Select all
val a = if (b % 2 == 0) {2} else {1}


Also, by the way, a feature that a future release of python could implement is a __call__-method in a boolean, such that one can do:
Code: Select all
a = (b % 2 == 0)(2, 1)


Lazy evaluation is still possible by using lambda's, so it becomes:
Code: Select all
a = (b % 2 == 0)(lambda: 2, lambda: 1)()


The closest syntax so far is:
Code: Select all
a = [lambda: 2, lambda: 1][b % 2]()

but that reads backwards
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 82
Joined: Tue Feb 12, 2013 8:30 pm


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests