Attack a sacred Python Cow

Carl Banks pavlovevidence at gmail.com
Sat Jul 26 18:58:16 EDT 2008


On Jul 26, 5:07 pm, Terry Reedy <tjre... at udel.edu> wrote:
> Whether or not one should write 'if x' or 'if x != 0' [typo corrected]
> depends on whether one means the general 'if x is any non-null object
> for which bool(x) == True' or the specific 'if x is anything other than
> numeric zero'.  The two are not equivalent.  Ditto for the length example.

Can you think of any use cases for the former?  And I mean something
where it can't be boiled down to a simple explicit test for the sorts
of arguments you're expecting; something that really takes advantage
of the "all objects are either true or false" paradigm.

The best thing I can come up with out of my mind is cases where you
want to check for zero or an empty sequence, and you want to accept
None as an alternative negative as well.  But that's pretty weak.

The use case of simply passing something to a function that accepts
any boolean doesn't count.  For instance, I could write:

def nand(a,b):
    return not (a and b)

And then I could use it like this, even if x is an interger and y a
string:

if nand(x,y):

But that doesn't buy much since I could just pass in the explicit
tests like this:

if nand(x!=0,y!=""):


Carl Banks



More information about the Python-list mailing list