5

I am stuck at this particular example from dive into python

Example 4.18. When the and−or Trick Fails

>>>>a = "" >>>>b = "second" >>>1 and a or b >>>>'second' 

Since a is an empty string, which Python considers false in a boolean context, 1 and '' evalutes to '', and then '' or 'second' evalutes to 'second'. Oops! That's not what you wanted. The and−or trick, bool and a or b, will not work like the C expression bool ? a : b when a is false in a boolean context.

Why does it says it isn't what the user wants, I mean 1 and "" would evaluate to False, while "" or b will evaluate to "second", that's perfectly what should happen, I don't understand why is it wrong?am I missing something?

4 Answers 4

4

No, you're not missing something.

The expr if cond else expr inline conditional was introduced to Python 2.5 in PEP 308; before that, conditionals had to be of the full form

if cond: expr else: expr 

However, people noticed that expr and cond or expr sort-of worked as an inline conditional, as long as the first expressions happened to evaluate to boolean true. As most things in Python are true, this syntax generally tended to work -- and then, of course, sometimes fail. This was in fact the motivation for adding the "proper" inline conditional.

Sign up to request clarification or add additional context in comments.

Comments

2

The "and-or trick" was used to emulate ternary conditional operator, condition ? if-true : if-false or similar. Since 1 is equivalent to True in boolean context, the expression is expected to yield a, not b. So, the lesson is, don't use and-or trick. Python has ternary conditional since 2.5:

true if condition else false 

1 Comment

Thank you.I thought there is something else which I don't understand.
2

Dive into python is not a good tutorial and should not be followed


The x and y or z expression was used before python had a proper ternary expression (y if x else z). This section is saying why it can surprise you if you expect it to behave like C's x ? y : z.


There's a list of tutorials on the python wiki. that might give you a more up to date resource.

4 Comments

what should I follow then?, I thought dive into python should suffice as examples.I am aware of the syntax, I was searching for example to see how things can be done better with python
@KartikAnand: The link in my answer has many options under the "Learning Python" section (where you may notice that DIP is not present in any form.)
Regarding the rant, what exactly has replaced ODBC?
@KarlKnechtel: I think this has info on it: wiki.python.org/moin/DatabaseProgramming
0

Why does it says it isn't what the user wants, I mean 1 and "" would evaluate to False, while "" or b will evaluate to "second", that's perfectly what should happen, I don't understand why is it wrong?am I missing something?

I feel this part of the question is being ignored. condition and if-true or if-false is a trick used in versions >2.5, as explained in other answers, to mimic other languages' condition ? if-true : if-false or later versions' if-true if condition else if-false.

The problem with it is that if if-true is an object that evaluates to false ("", [], (), False or 0), then this would fail, as expected when you look at the evaluation, but which might creep in as a bug if if-true is dynamically set and can evaluate to false.

The naively expected result of condition and a or b would have been the contents of a ("", empty string), as the condition (1) evaluates to true, but as 1 and a evaluates to false so you get the contents of b.

The solution to this problem is to either use the Python 2.5+ syntax (if-true if condition else if-false) or make sure that if-true is never false. One way to accomplish the second is to pack if-true and if-false in a tuple or list, as a nonempty iterable is always true:

>>> a = "" >>> b = "second" >>> condition = 1 >>> condition and a or b 'second' >>> condition and [a] or [b] [''] >>> (condition and [a] or [b])[0] '' 

It's a kind of ugly solution, but it's there if you need to target old versions of Python.

2 Comments

actually what you said is already given in the book, and my question is not how to rectify the bug, my question is why does the author say "it isn't what the user expected".Actually you answered my question in the first part of your answer, there was no need of code and thanks :)
@KartikAnand: sure, I just wanted to make sure that if somebody finds this question in the future, they'll have the full context :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.