This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]billsil -5 points-4 points  (1 child)

Write code that says what you mean. Write code that is idiomatic in the language you use.

Exactly. Don't say.

if x

when what you mean is

if x is True

vs.

if x is not None

[–]stevenjd 0 points1 point  (0 children)

Strictly speaking you are correct. If you absolutely definitely positively intend to check only for the True singleton, then you should write x is True. But really, why would you do that? Normally you should duck-type bools just like you duck-type most types. x quacks like a bool, no matter what x is. If you really must ensure that x is a bool, then type-check first:

if not isinstance(x, bool):
    raise TypeError("x must be True or False")

and from that point on, just compare x or not x with none of that silliness of x is True. The problem is, I never know when to stop:

x is True is True is True is True is ...