Boolean tests [was Re: Attack a sacred Python Cow]

Heiko Wundram modelnine at modelnine.org
Wed Jul 30 09:49:34 CEST 2008


Am Mittwoch, 30. Juli 2008 09:18:48 schrieb Russ P.:
> Oh, Lordy. I understand perfectly well how boolean tests, __len__, and
> __nonzero__ work in Python. It's very basic stuff. You can quit
> patronizing me (and Carl too, I'm sure).

I'll stop repeating what the current state is (which might sound like I'm 
patronizing, but that's not the real intent actually, I'm just trying to get 
the discussion straight with what is fact, namely that there already exists 
an explicit way which doesn't seem to be recognized by some people here) if 
you agree to my point that we're not talking about a problem with Python, but 
about a personal stylistic issue with the language design. That's just what I 
said in my last mail: I can concede that you, personally, have an issue with 
the current state, but for me and seemingly also for a majority of people who 
have posted in this thread, that's a non-issue.

> The point that you seem to be missing, or refuse to acknowledge for
> some reason, is that  "if x" can be mistakenly applied to any object
> when the programmer thinks that x is a list -- and the programmer will
> receive no feedback on the error.
>
> I have made errors like that, and I could have saved some time had I
> used an "empty" method that only applies to a list or other sequence.

For me, I've never had this kind of problem, simply because if I test for the 
truth value of something, I'm going to do something with it later on, and as 
soon as I'm doing something with it, I'll see whether the object supports the 
interface I want it to support (and get an error there if the object doesn't 
support the basic notions of a sequence type, for example, i.e. __iter__()). 
Testing for truth is IMHO not doing something with an object, and as such I'm 
more than happy that it's "foolproof". This is one thing that I personally 
find attractive about Python's way of duck-typing.

But, if you personally have been bitten by this, give an example, and I'm sure 
that we can start discussing from there. I've already given an example why 
the explicit test for length is less polymorphic than the explicit test for 
truth of a container elsewhere as a proof for the point I'm trying to make.

-- 
Heiko Wundram



More information about the Python-list mailing list