
{"id":4767,"date":"2020-09-26T01:26:49","date_gmt":"2020-09-26T05:26:49","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4767"},"modified":"2020-09-27T00:45:12","modified_gmt":"2020-09-27T04:45:12","slug":"python-logical-operations-with-none-and-booleans","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4767","title":{"rendered":"Python: logical operations with None and Booleans"},"content":{"rendered":"<p>Coming back to the blog after long break.<\/p>\n<p>Today, almost by accident I found how logical operators work with <code>None<\/code> in Python. The result is surprising, because it is not commutative.<\/p>\n<pre>\r\n(False or None)  is None\r\n(None  or False) is False\r\n(True  or None)  is True\r\n(None  or True)  is True\r\n\r\n(False and None)  is False\r\n(None  and False) is None\r\n(True  and None)  is None\r\n(None  and True)  is None\r\n<\/pre>\n<p>There seems to be little logic here (pun intended), but in fact it all makes sense. For &#8220;or&#8221;, if the first operand is truthy, it is returned, and the second operand is not calculated. If the first operant is falsy, the second operand is returned. For &#8220;and&#8221;, if the first operand is falsy, it is returned, and if it is truthy, the second operand is returned. The definitions are not symmetrical, which results in non-commutative behavior with <code>None<\/code>.<\/p>\n<p>UPDATE: Javascript behaves the same way with <code>null><\/code>. E.g. <code>(null && false) === null<\/code>, but <code>(false && null) === false<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coming back to the blog after long break. Today, almost by accident I found how logical operators work with None in Python. The result is surprising, because it is not <a href=\"https:\/\/ikriv.com\/blog\/?p=4767\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-4767","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4767"}],"version-history":[{"count":4,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4767\/revisions"}],"predecessor-version":[{"id":4774,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4767\/revisions\/4774"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}