Implicit conversion to boolean in if and while statements

Chris Angelico rosuav at gmail.com
Mon Jul 16 03:51:13 CEST 2012


On Mon, Jul 16, 2012 at 11:21 AM, Ranting Rick
<rantingrickjohnson at gmail.com> wrote:
> If HOWEVER we want to "truth test" an object (as in: "if obj") we
> should be FORCED to use the bool! Why? Because explicit is better than
> implicit and readability counts if we want to create maintainable code
> bases!
>
> if bool(obj) and a==b: # Correct!
> if obj and a==b:       # Incorrect!

That still doesn't answer the question of what bool(obj) should do if
obj is not a bool, and why if can't do the exact same thing, since if,
by definition, is looking for a boolean state selector.

ChrisA



More information about the Python-list mailing list