Use of and instead

I’m doing a course on web application development on Udacity. I noticed that the instructor uses the and operator in the return in my validation method. And I did not understand how to return 2 arguments. I think it could be something like an if . Can someone explain what this really is?

Here is a verification method:

 USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$") def valid_username(username): return username and USER_RE.match(username) 

Thanks in advance.

+7
source share
4 answers

The and operator evaluates whether both of its arguments are tru-ish, but in a somewhat surprising way: first, it parses the left argument. If this is true, then it returns its correct argument. If the left argument is false, it returns the left argument.

So the last line in your code:

 return username and USER_RE.match(username) 

matches with:

 if username: return USER_RE.match(username) else: return username 

Lines like username are true if they are not empty. The regex match function returns a true match object if the pattern matches, and returns None falsish if it does not match.

The end result is that valid_username will return true if username is not an empty string and the username matches the given pattern.

Note that the β€œand” here has nothing to do with returning two values, computing a single value.

+12
source

When you use a logical operator, it continues in accordance with the rules, so with and, it evaluates the veracity of the first operator and, if it is not truthful, it returns an implausible value (in the first case, ``).

 print repr("" and "THIS IS POST AND") "" print "" or "THIS IS POST AND" THIS IS POST AND print None or "Something else" Something else 

If this is convenient for you, this is when you do not want to call a non-existent method on something like None (for example, length):

 r = None s = [1,2,3,4] print r and len(r) None print s and len(s) 4 

In the case when you sent a message, the fact is that you want to check the username only for regular expression, if the username is true.

It is important to note here that and and or both short circuits. Therefore, if you get something implausible, the function will not even evaluate the regular expression.

+2
source

and is just a binary operator. return a and b structurally matches return a + b or return a * b and works the same way. The expression is evaluated, then its result is assigned to return , which returns it to the caller. There is nothing special about using and in a return .

As Ned's answer explained well, the result of a and b is true-ish if both of a and b are true, and false otherwise. It also closes if the left argument is false, as this is enough to determine the result of the whole expression a and b .

In this case, an empty string will not match this regular expression, so the operation redundant is considered purely logical. I strongly suspect that it is used here because username can be set to None , which will cause the regex to throw an exception. Using username and USER_RE.match(username) , and not just USER_RE.match(username) means that any false-ish value (including None ) for username causes the function to return something false-ish without even trying to match the regular expression.

But then again, this has nothing to do with return , it's just how and works.

0
source

For those who are starting to learn python, I find this statement difficult to understand.

return username and USER_RE.match (username)

what we should notice in the first place is that this method will return a boolean (either return False or True) it does not return the string "username"

so a simple understanding is this: if the username is not empty and the username is validated for the regular expression, return True, otherwise return False

0
source

All Articles