On 09/08/12 08:37, Alexander Belopolsky wrote:
With respect to bool, an argument can be made that it having only two preallocated instances is a language feature rather than an implementation detail and user can rely on the fact that True, bool(1) and say not [] all return the same instance. If this is so, I think this fact should be mentioned somewhere, but probably not in PEP 8.
Yes, this is documented: http://docs.python.org/py3k/library/functions.html#bool However, the question is not whether if x is True: and if isinstance(x, bool) and x: are equivalent, or whether there are obscure edge-cases where one genuinely needs to test whether or not an object is identical to True. The question is what is the idiomatic Python code for testing a flag. That is simply if x: which duck-types truthiness and accepts any true value, not just True. Like almost all identity tests, testing "x is True" puts the emphasis on the wrong thing: object identity instead of object value. It restricts the value's type instead of duck-typing, and that's usually unpythonic. If you do have a special need to do so, okay, that's fine, but there's no need for PEP 8 to cover your special use-case. PEP 8 is for conventions for idiomatic code, not every exception. PEP 8 already gives its blessing to break the rules when you need to. Besides, even when it is not, "x is True" looks like a rookie mistake. Beginners often write "if flag == True" even in statically-typed languages where flags cannot be other than a bool. -- Steven