为什么`True是False == False`,在Python中是假的?

时间:2015-07-11 07:03:35

标签: python python-3.x

为什么在使用括号时这些语句按预期工作:

>>> (True is False) == False
True

>>> True is (False == False)
True

但是当没有括号时会返回False

>>> True is False == False
False

5 个答案:

答案 0 :(得分:15)

基于python documentation关于运算符优先级:

  

请注意,比较,成员资格测试和身份测试都具有相同的优先级,并具有从左到右链接功能,如比较部分所述。

所以实际上你有一个链式声明如下:

>>> (True is False) and (False==False)
False

您可以假设中心对象将在2个操作和其他对象之间共享(在这种情况下为False)。

请注意,对于所有比较,它也适用,包括跟随操作数的成员资格测试和身份测试操作:

in, not in, is, is not, <, <=, >, >=, !=, ==

示例:

>>> 1 in [1,2] == True
False

答案 1 :(得分:10)

Python在比较运算符方面具有独特的传递属性。在更简单的情况下会更容易看到。

if 1 < x < 2:
    # Do something

这就是它的样子。它检查1 <1。 x和如果x < 2.你的非括号内的代码也会发生同样的事情。

>>> True is False == False
False

检查True是否为假 False == False,其中只有一个为真。

答案 2 :(得分:8)

这是一个双重不等式,它被扩展为xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" > </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_below="@+id/toolbar" android:layout_width="match_parent" android:layout_height="48dip" android:background="@drawable/background_tabs" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/tabs" tools:context=".MainActivity" /> 。例如,请参阅What is the operator precedence when writing a double inequality in Python (explicitly in the code, and how can this be overridden for arrays?)

答案 3 :(得分:2)

Python以您在Math中所期望的方式解释多个(in)等式:

数学 HTTP Status 500 - javax.servlet.ServletException: java.lang.ExceptionInInitializerError type Exception report message javax.servlet.ServletException: java.lang.ExceptionInInitializerError description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ExceptionInInitializerError org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause javax.servlet.ServletException: java.lang.ExceptionInInitializerError org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845) org.apache.jsp.index_jsp._jspService(index_jsp.java:138) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause java.lang.ExceptionInInitializerError softdev.asese.utils.HibernateUtil.<clinit>(HibernateUtil.java:17) softdev.asese.dao.GuardianDao.getGuardian(GuardianDao.java:16) org.apache.jsp.index_jsp._jspService(index_jsp.java:93) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: softdev.asese.models.AssessmentModel.Student in softdev.asese.models.StudentModel.assessments org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:769) org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:729) org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70) org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697) org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426) org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930) softdev.asese.utils.HibernateUtil.<clinit>(HibernateUtil.java:14) softdev.asese.dao.GuardianDao.getGuardian(GuardianDao.java:16) org.apache.jsp.index_jsp._jspService(index_jsp.java:93) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 表示所有a = b = ca = bb = c

所以a = c表示True is False == FalseTrue == False以及False == False,即True == False

对于布尔常量,False等同于is

答案 4 :(得分:1)

如果在评估表达式时遇到具有相同优先级的运算符,Python会执行链接。

  

比较,包括测试,都具有相同的优先级   从左到右

下面提到的运算符具有相同的优先级。

in, not in, is, is not, <, <=, >, >=, <>, !=, ==

因此,当Python尝试评估表达式True is False == False时,它会遇到具有相同优先级的运算符is==,因此它会从左到右执行链接。

因此,表达式True is False == False实际上被评估为:

(True is False) and (False == False)

False作为输出。