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

Steven D'Aprano steve at
Wed Jul 30 01:59:50 CEST 2008

On Tue, 29 Jul 2008 13:53:07 -0700, Carl Banks wrote:

> Bzzt.  "if len(x)!=0" is a simple explicit that would work for this
> class and all built-in containers. (Or should--Steven D'Aprano's
> objections notwithstanding, any reasonable container type should support
> this invariant.

What's the length of an empty list with a sentinel at the end?

What's the length of a binary tree? Does it even have a length?

What's the length of a constant-sized table where each position might be 
in use or free?

You're making assumptions about what an empty object "should" look like. 
I say, just ask the object if it's empty, don't try to guess how you 
would find out. Maybe the object carries around a flag x.isempty. Who 
knows? Why do you care? You're basing your code on implementation 
details. That should be avoided whenever possible.


> Once again, I'm invoking the contraint against simply using x in a
> boolean context, 

So, to put it another way... you want us to give you an example of using 
x in a boolean context, but you're excluding any example where we use x 
in a boolean context.

Right-o, I've had enough of your trolling. I'm out of here. 


More information about the Python-list mailing list