True inconsistency in Python

Michael Geary Mike at DeleteThis.Geary.com
Thu Nov 13 03:19:31 EST 2003


Ron Adam:
> The only thing that surprises me in all of this is the
> "if var:" evaluating to true for numbers other than 1.
> That's new to me,  I would have expected an
> exception in that case.  But it still makes since if I
> look at it as a shortcut for  "if (var != 0):".    This only
> proves I'm still relatively new to Python I think.

It's handy, and natural in many real life situations, to treat any nonzero
value as "true". For example:

Do you have any money in your wallet?

Do you have children?

I could ask how much money or how many children you have, but if I just ask
the yes-or-no question, you'll naturally answer "yes" for any nonzero value.

Of course, if you have children, you won't have money, but that's a separate
problem...

-Mike






More information about the Python-list mailing list