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

Russ P. Russ.Paielli at gmail.com
Wed Jul 30 03:53:47 EDT 2008


On Jul 30, 12:49 am, Heiko Wundram <modeln... at modelnine.org> wrote:
> 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

Fair enough. I have no dog in this particular fight. I just think it
wouldn't hurt to add an "isempty()" or "isnonempty()" method to the
list type, and let people use it if they wish, or continue using "if
x" if that's what they prefer.



More information about the Python-list mailing list