Python three-object operations, and or traps

Source: Internet
Author: User

In C language, three-object operations are often used (cond? A: B). It is very concise, but this syntax is not supported in Python.
However, it can be implemented using and or in Python (there is a trap here, which will be discussed below)
Let's look at the following expressions:
>>> False or 1
1
>>> False or 0
0
>>> True or 0
True
>>> True and 1
1
>>> True and 0
0
>>> False and 1
False

That is to say, the and or expression returns a final expression result, instead of simply True or False. The following expressions are used:
>>> True and 1 or 2 # TRUE?
1
>>> False and 1 or 2 # TRUE?
2

Is this similar to the result of the Three-object operation?
Here you may think of the Three-object expression at the beginning of the article, which can be expressed in this way.
>>> Cond and a or B

Yes, this expression is correct in most cases, but the traps mentioned above also appear here
>>> Cond, a, B = True, 0, 1 # After the value is assigned
>>> Cond and a or B # What is the result of this expression?
1 # That is, B, not the expected

This is because, in the and or operation, the Null String '', number 0, empty list [], empty dictionary {}, empty (), None, all operations are processed as false, as shown in the following example.
>>> 'Ss' and ''or 'bbb'
'Bbb'

But don't worry. There is still a solution.
The following expression is provided in the article "an expression equivalent to the three-object operator in C language ".
>>> C = cond and a or (not cond or a) or B # It seems a bit complicated.

The following method is provided in Dive into Python:
>>> (1 and [a] or [B]) [0] # (cond and [a] or [B]) [0]

Because, even if a or B is a logical False value, it is False after it is put into the set, that is, [False] [None] is not False.

In addition, I think it can be implemented in this way.
>>> (B, a) [cond and 1 or 0] # The idea is that the positions of a and B in Tuble are reversed.

In addition, the article "Shenzhen night returnee" provides a way to use dict.
>>> Max_ AB = {True: a, False: B} [a> B]

However, this method is not universal, because the value of cond is not necessarily set to True or False.

Therefore, we recommend that you use the Dive into Python method or my method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.