这是什么"和"声明实际上做的回报? [英] What is this "and" statement actually doing in the return?

查看:140
本文介绍了这是什么"和"声明实际上做的回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地理解下面的Python code的,为什么笔者已经用在返回的AND语句。

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):
        PASS_RE = re.compile(r'^.{6,128}$')
        return password and PASS_RE.match(password)

进一步下跌的code ...

further down the code...

if not self.valid_password(self.password):
    params['error_password'] = "Please enter a valid password."

我试过检查了被交回给调用者生成的对象,但我还是不完全了解它是如何工作的。

I've tried inspecting the resulting object that gets handed back to the caller, however I still don't entirely understand how it works.

看起来这会返回一个口令返回给调用者和密码是否有效的一个布尔值,但是我不明白的调用函数如何检查对象的布尔?这是一些基本的关于Python,我已经错过了吗?

It seems like this returns the password back to the caller and a boolean of whether or not the password is valid, however I don't understand how the calling function can check the bool of an object? Is this something basic about Python that I've missed?

有旁边这一个类似的使用的另一个例子但是它使用或声明,对我来说是更加混乱:

There is another example of a similar usage next to this one however it uses the "or" statement which to me is even more confusing:

def valid_email(self, email):
    EMAIL_RE  = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
    return not email or EMAIL_RE.match(email)

这到底是怎么回事就在这里将是极大的AP preciated任何意见。在code ++工程,并做了你希望它做什么,验证对一个普通的前pression输入和返回真或假,但是我真的想了解它是这样写的,而不是简单地返回在布尔。

Any advice on exactly what is going on here would be greatly appreciated. The code works and does what you would expect it to do, validates the input against a regular expression and returns True or False, however I would really like to understand what it was written like this and not simply returning the bool.

推荐答案

在Python中,无论将返回其操作数之一。随着,巨蟒检查第一个操作数,如果它是一个truthy值(更多关于感实性更高版本),它返回而不检查第二个是第一个值(这是称为布尔快捷的评价,并且它可能是重要的)。如果第一个是falsey,那么Python返回第二个操作数,不管它是什么:

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  2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 or 3
2
>>> 0 or 3
3

使用和,类似的事情发生了:第一个操作数是最先被检查,如果是falsey,那么Python从来不检查第二个操作数。如果第一个操作数是truthy,那么Python返回第二个操作数,不管它是什么:

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:

>>> 2 and 3
3
>>> 0 and 3
0
>>> 3 and 0
0
>>> 3 and []
[]
>>> 0 and []
0

现在让我们谈谈感实性和falsiness。 Python使用下列规则在布尔环境评估的事情:

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


  • 下面的值是falsey:虚假,无,0(零),[](空单),()(空数组),{}(空字典),一个空集, (空字符串)

  • 一切是truthy

因此​​,像密码和PASS_RE.match(密码)正在利用Python的短路评价。如果密码是无,那么运营商将只返回无永不评估下半年。这是很好的,因为 PASS_RE.match(无)会抛出异常。关注此:

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:

>>> 3 or []
3
>>> [] or 3
3
>>> 0 or []
[]
>>> [] or 0
0
>>> 0 and []
0
>>> [] and 0
[]

请参阅如何短路工作?现在看这样的:

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.

这篇关于这是什么&QUOT;和&QUOT;声明实际上做的回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆