Implicit conversion to boolean in if and while statements

Chris Angelico rosuav at gmail.com
Mon Jul 16 01:57:39 EDT 2012


On Mon, Jul 16, 2012 at 2:53 PM, Ranting Rick
<rantingrickjohnson at gmail.com> wrote:
> "if obj" is in essence doing "if bool(obj)" behind the scenes. My
> question is: Why hide such valuable information from the reader? It's
> obvious that "if bool(obj)" will return a boolean; whereas "if obj" is
> ambiguous.

Proves nothing. At least when there are less names used, there's less
possibility of monkey-patching.

>>> def bool(n):
...     return 5
...
>>> if bool([]):
...     print("Yay?")
...
Yay?

ChrisA



More information about the Python-list mailing list