Skip to content Skip to sidebar Skip to footer

What Is This "and" Statement Actually Doing In The Return?

I am trying to get a better understanding of the following python code and why the author has used the 'AND' statement in the return. def valid_password(self, password): PA

Solution 1:

In Python, both and and or will return one of their operands. With or, Python checks the first operand and, if it is a "truthy" value (more on truthiness later), it returns the first value without checking the second (this is called Boolean shortcut evaluation, and it can be important). If the first is "falsey", then Python returns the second operand, no matter what it is:

Python 2.7.3 (default, Jan  22013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits"or"license"for more information.
>>> 2or32
>>> 0or33

With "and", much the same thing happens: the first operand is checked first, and if it is "falsey", then Python never checks the second operand. If the first operand is "truthy", then Python returns the second operand, no matter what it is:

>>>2and3
3
>>>0and3
0
>>>3and0
0
>>>3and []
[]
>>>0and []
0

Now let's talk about "truthiness" and "falsiness". Python uses the following rules for evaluating things in a Boolean context:

  • The following values are "falsey": False, None, 0 (zero), [] (the empty list), () (the empty tuple), {} (the empty dict), an empty set, "" (the empty string)
  • Everything else is "truthy"

So something like password and PASS_RE.match(password) is taking advantage of Python's short-circuit evaluation. If password is None, then the and operator will just return None and never evaluate the second half. Which is good, because PASS_RE.match(None) would have thrown an exception. Watch this:

>>>3or []
3
>>>[] or3
3
>>>0or []
[]
>>>[] or0
0
>>>0and []
0
>>>[] and0
[]

See how the short-circuiting is working? Now watch this:

>>>value = "hello">>>print (value.upper())
HELLO
>>>print (value and value.upper())
HELLO
>>>value = None>>>print (value.upper())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'upper'
>>>print (value and value.upper())
None

See how the short-circuiting feature of and helped us avoid a traceback? That's what's going on in this function.

Solution 2:

The line in question verifies that the password is 'truthy' and that it matches a predefined password regular expression.

Here's how it breaks down:

  • The function returns password if password is 'falsey'.

  • If the password is 'truthy', but the password does not match the password regex, the function returns None.

  • If there is a 'truthy' password, and it matches the regex, the match object is returned.

The and operator is a 'short-circuit' operator; If the first value is 'truthy', it returns the second value. Otherwise, it return the first value.

You can check out this page to see what types of things are 'truthy' in python.

Solution 3:

You just need to know that:

  1. Empty strings in Python evaluates to False (also None, empty lists, and other "zero" types).
  2. Boolean expressions are subject to an optimization called short-circuit evaluation

Therefore,

a = '1'print('' and a)

... prints the empty string because as it is False, the expression can never be True and the second part (the a) is never even evaluated.

And

a = '1'print('' or a)

prints '1', because the empty string is False, the second part has to be evaluated to give the result of the expression.

Post a Comment for "What Is This "and" Statement Actually Doing In The Return?"