The elegant Python-and_or Dafa

Source: Internet
Author: User

Objective

George Boole magically turns logic into algebra so that it can be counted, so it is called Boolean algebra number. And and and or are the 2 symbols of Boolean algebra.


first of all, we want to make it clear that Python, like many languages, returns one of two manipulated objects, not their Boolean expression True or False.

Like what

>>false and 2false>>true and 22>>1 and 11

second, understand what is the short circuit logic.
The short-circuit logic means that the back is not performed. There are two cases.
1.x and Y, if X is false, x and y must be false without having to control what Y is.
2.x or Y, if x is true, x or y must be true without having to control what Y is.


What are the applications of And_or Dafa?

or, to set the default valueFor example, we get JSON-based dictionary objects from Redis, to prevent data from appearing valueerror:no JSON object could be decoded.
We will generally
If data:
Json.loads (data)
Using And_or Dafa, you can directly json.loads (data or ' {} ')To set the default value by or.


and, use the preceding to true to execute the back, to destroy if

such as e-commerce, customers to buy things, through multi-criteria screening, data exist MongoDB, attributes have price/brand/size/color.
The user may not set or set multiple filter criteria, and we want to judge each one.
According to the general wording, it should be:

QUERYEXR = {}if Price:    queryexr.update ({' P ':p rice}) If Brand:    queryexr.update ({' B ': brand}) if size:    Queryexr.update ({' s ': size}) If color:    queryexr.update ({' c ': color}) Dp.collection.find (QUERYEXR)
Too much if, looking at headaches, using and_or Dafa:

Price and Queryexr.update ({' P ':p rice}) brand and Queryexr.update ({' B ': brand}) size and  queryexr.update ({' s ': size} ) color and queryexr.update ({' c ': color})

Implementing three-mesh conditional characters

C language has three mesh condition <condition>? <expression1>: <expression2>
Python can achieve the same purpose by <expression1> if <condition> else <expression2>
In addition, Python can also use And_or Dafa, <condition> and <expression1> or <expression2>
How do you understand this expression?
such as age>=18 and ' adult ' or ' kid '.
Equivalent to ((Age>=18 and ' adult ') or ' kid '), where <condition> is age>=18.
If <condition> is true, the result of (age>=18 and ' adult ') is ' adult ', the result of (' adult ' or ' kid ') is ' adult ', so the whole result is ' adult '
If <condition> is false, the result of (age>=18 and ' adult ') is false, (false or ' kid ') result is ' kid ', so the whole result is ' kid '

PS. Python the two styles of implementing the three-mesh condition may be somewhat confusing, and the summary is that if you use if...else ... The Dafa,<condition> is behind the IF, and if And_or Dafa,<condition> in the front, the order is consistent with the C-style three-mesh operator.



The elegant Python-and_or Dafa

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.