do you know if Python supports some keyword or expression like in C++ to return values based on if condition, all in the same line (The C++ if expressed with the question mark ?)
// C++
value = ( a > 10 ? b : c )
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For Python 2.4 and lower you would have to do something like the following, although the semantics isn’t identical as the short circuiting effect is lost:
There’s also another hack using ‘and … or’ but it’s best to not use it as it has an undesirable behaviour in some situations that can lead to a hard to find bug. I won’t even write the hack here as I think it’s best not to use it, but you can read about it on Wikipedia if you want.