<legend id='MIPi3'><style id='MIPi3'><dir id='MIPi3'><q id='MIPi3'></q></dir></style></legend>

      • <bdo id='MIPi3'></bdo><ul id='MIPi3'></ul>

      <small id='MIPi3'></small><noframes id='MIPi3'>

      <i id='MIPi3'><tr id='MIPi3'><dt id='MIPi3'><q id='MIPi3'><span id='MIPi3'><b id='MIPi3'><form id='MIPi3'><ins id='MIPi3'></ins><ul id='MIPi3'></ul><sub id='MIPi3'></sub></form><legend id='MIPi3'></legend><bdo id='MIPi3'><pre id='MIPi3'><center id='MIPi3'></center></pre></bdo></b><th id='MIPi3'></th></span></q></dt></tr></i><div id='MIPi3'><tfoot id='MIPi3'></tfoot><dl id='MIPi3'><fieldset id='MIPi3'></fieldset></dl></div>
    1. <tfoot id='MIPi3'></tfoot>

      这个“和"是什么?声明中究竟在做什么?

      What is this quot;andquot; statement actually doing in the return?(这个“和是什么?声明中究竟在做什么?)
    2. <i id='gYNab'><tr id='gYNab'><dt id='gYNab'><q id='gYNab'><span id='gYNab'><b id='gYNab'><form id='gYNab'><ins id='gYNab'></ins><ul id='gYNab'></ul><sub id='gYNab'></sub></form><legend id='gYNab'></legend><bdo id='gYNab'><pre id='gYNab'><center id='gYNab'></center></pre></bdo></b><th id='gYNab'></th></span></q></dt></tr></i><div id='gYNab'><tfoot id='gYNab'></tfoot><dl id='gYNab'><fieldset id='gYNab'></fieldset></dl></div>
        <tfoot id='gYNab'></tfoot>
        • <bdo id='gYNab'></bdo><ul id='gYNab'></ul>

                  <tbody id='gYNab'></tbody>
                <legend id='gYNab'><style id='gYNab'><dir id='gYNab'><q id='gYNab'></q></dir></style></legend>

                <small id='gYNab'></small><noframes id='gYNab'>

                本文介绍了这个“和"是什么?声明中究竟在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我试图更好地理解以下python代码以及作者为什么在return中使用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)
                

                进一步的代码...

                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)
                

                任何关于这里到底发生了什么的建议将不胜感激.该代码可以正常工作并执行您期望它执行的操作,根据正则表达式验证输入并返回 True 或 False,但是我真的很想了解它是这样写的,而不是简单地返回 bool.

                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 中,andor 都将返回它们的操作数之一.使用 or,Python 会检查第一个操作数,如果它是真实"值(稍后将详细介绍真实性),它会返回第一个值而不检查第二个值(这称为布尔快捷评估,并且这可能很重要).如果第一个是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
                

                使用and",会发生同样的事情:首先检查第一个操作数,如果它是假",那么 Python 永远不会检查第二个操作数.如果第一个操作数是真",那么 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
                

                现在我们来谈谈真"和假".Python 使用以下规则在布尔上下文中评估事物:

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

                • 以下值为falsey":False、None、0(零)、[](空列表)、()(空元组)、{}(空字典)、空集、"(空字符串)
                • 其他一切都是真实的"

                所以像 password 和 PASS_RE.match(password) 这样的东西正在利用 Python 的短路评估.如果 password 为 None,则 and 运算符将只返回 None 并且永远不会计算后半部分.这很好,因为 PASS_RE.match(None) 会引发异常.观看:

                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.

                这篇关于这个“和"是什么?声明中究竟在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  • <bdo id='Hz41U'></bdo><ul id='Hz41U'></ul>
                      <legend id='Hz41U'><style id='Hz41U'><dir id='Hz41U'><q id='Hz41U'></q></dir></style></legend>
                        <tbody id='Hz41U'></tbody>
                    1. <tfoot id='Hz41U'></tfoot>
                      1. <small id='Hz41U'></small><noframes id='Hz41U'>

                          <i id='Hz41U'><tr id='Hz41U'><dt id='Hz41U'><q id='Hz41U'><span id='Hz41U'><b id='Hz41U'><form id='Hz41U'><ins id='Hz41U'></ins><ul id='Hz41U'></ul><sub id='Hz41U'></sub></form><legend id='Hz41U'></legend><bdo id='Hz41U'><pre id='Hz41U'><center id='Hz41U'></center></pre></bdo></b><th id='Hz41U'></th></span></q></dt></tr></i><div id='Hz41U'><tfoot id='Hz41U'></tfoot><dl id='Hz41U'><fieldset id='Hz41U'></fieldset></dl></div>