I'd be strongly against changing that rule. If you don't want other types than bool, use an isinstance check. Code using "is True" is most likely a bug. (It's different for None, since that has no second value that is assumed.)<span></span><div>
<br></div><div>--Guido<br><br>On Wednesday, August 8, 2012, Michael Foord wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hey all,<div><br></div><div>True and False are singletons, so if you want to check precisely for True and False then an identity check seems appropriate. </div>
<div><br></div><div>However the "Programming Recommendations" section of PEP 8 has this to say on the topic:</div>
<div><br></div><div><div>Don't compare boolean values to True or False using ==.</div><div><br></div><div>Yes: if greeting:</div><div>No: if greeting == True:</div><div>Worse: if greeting is True:</div><div><br>
</div>
<div><a href="http://www.python.org/dev/peps/pep-0008/#programming-recommendations" target="_blank">http://www.python.org/dev/peps/pep-0008/#programming-recommendations</a></div><div><br></div><div>It seems to me that there is an important distinction between testing that an object is either the boolean True or False and merely checking the "truthiness" of an object. Many a bug has been caused by an empty container object (or some other falsey object) falling into an "if not value" clause that was actually meant to check for the presence of False or None.</div>

<div><br></div><div>Why does PEP 8 recommend not testing "boolean values" (which to me implies "values you expect to be a bool") using equality or identity, and more specifically why does it say that using an identity check is worse than an equality check?</div>

<div><br></div><div>As this is Python-ideas and not python-list, my specific suggestion is to modify the wording of that section - or just removing it altogether as I don't think it can be adequately clarified without using lots of words.</div>

<div><br></div><div>All the best,</div><div><br></div><div>Michael Foord</div><div><br></div>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>

May you find forgiveness for yourself and forgive others<br>May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>