[Turn] and or skills

4.6.  And  and  or  special character

In Python,, and  and  or  perform boolean logic calculus, as you would expect the same. But they do not return a Boolean value, but they return one of the actual value comparison.

Example 4.15.  And  Introduction

>>> 'a' and 'b'       
'b'
>>> '' and 'b' '' >>> 'a' and 'b' and 'c' 'c'
1 Use  and  when, from left to right value calculation expression in a boolean context. 0 , '' , [] , () , {} , None  of the environment in a boolean false; anything else is true. Fortunately, almost everything. By default, Environment class instance Boolean is true, but you can define a specific class such calculation method of the class instance is false. You will be in Chapter 5 learned about classes and special methods. If all Boolean environments are true, so  and  returns the last value. In this example, and  calculating  'a'  is true, then  'b'  calculus is true, eventually returns  'b' .
2 If a Boolean value is false environment, then  and  returns the first false value. In this example, ''  is a first false value.
3 All values are true, so  and  returns the last value true, 'C' .

Example 4.16.  Or  introduction

>>> 'a' or 'b'         
'a'
>>> '' or 'b' 'b' >>> '' or [] or {} {} >>> def sidefx(): ... print "in sidefx()" ... return 1 >>> 'a' or sidefx() 'a'
1 Use  or  when calculating the value of a boolean context from left to right, just  and  the same. If the value is true, or  returns that value immediately. In the present embodiment, 'A'  is the first true value.
2 or  calculus  ''  value is false, then  'b'  is true, then return to  'b'  .
3 If all values are false, or  returns the last false value. or  calculus  ''  value is false, then  []  is false, sequentially calculating  {}  is false, and returns  {}  .
4 Note  or  would have been carried out in a boolean context expressions calculus until the first true value, then it ignores the rest of the comparison value. If some values have side effects, this feature is very important. Here, the function  sidefx  never called, because  or  calculus  'a'  is true, it is followed immediately return to  'a'  up.

If you're a  C hacker language must be very familiar with the  bool  ?  A  :  b  expression, if  bool  is true, the expression evalutes  A , otherwise  b . Based on  Python,  and  and  or  work, you can accomplish the same thing.

4.6.1 The use  and-or  tricks

 Example 4.17.  And-or  tricks Introduction
>>> a = "first"
>>> b = "second"
>>> 1 and a or b 1 'first' >>> 0 and a or b 2 'second' 
1 This syntax looks similar to  the C language  BOOL  ?  A  :  b  expression. The entire expression from left to right calculus, first carried out  and  calculus expression. 1 and 'first'  evalutes  'First' , and  'first' or 'second'  calculus value  'First' .
2 0 and 'first'  evalutes  False , then  0 or 'second'  evalutes  'SECOND' .

However, since this  Python expression only just Boolean logic operations, not the specific language of the constitution, which is  and-or  tricks and  C language  BOOL  ?  A  :  b  very important difference grammar. If  a  is false, the expression will not work as you expect. (Can you tell I was tossing it over this issue? More than once?)

Example 4.18.  And-or  invalid skills occasion

>>> a = ""
>>> b = "second"
>>> 1 and a or b  'second'
1 Since  a  is an empty string, in the  Python environment hollow Boolean string are considered false, . 1 and '  calculus is  ' last  '' or 'second'  calculus value  'SECOND' . Oh! This value is not what you want.

and-or  skill, that is  BOOL  and  a  or  b  expression, when  a  value is false in a boolean context, and not as  the C language expression  BOOL  ?  a  :  b  work as.

In the  and-or  real trick behind the trick is to ensure that  a  value is never false. The most common way is to make  a  be  [ a ]  ,  b  become  [ b ] , then returns the first element of a list of values, should be  a  or  b is one.

Example 4.19. Safety  and-or  tricks

>>> a = ""
>>> b = "second"
>>> (1 and [a] or [b])[0]  ''
1 Because  [ A ]  is a non-empty list, it is never false. Even if  a  is  0  or  ''  or other false value, the list  [ a ]  is also true, because it has an element.

Until now, this trick may seem like more trouble than it's worth. After all, using the  if  statement can accomplish the same thing, so why go through all this fuss? Oh, in many cases, you have to choose between two constant values, because you know that  a  value is always true, so you can use this simple syntax and not worry. For more complicated safe form, there is good reason to do so. For example, in  some cases in Python  if  statements are not allowed, such as in  lambda  functions.

Guess you like

Origin www.cnblogs.com/a-cloud---/p/11204077.html