class with __len__ member fools boolean usage "if x:" ; bad coding style?

Greg Ewing greg at
Thu Jul 1 06:32:11 CEST 2004

Heiko Wundram wrote:
> ping_hosts = [h for h in hosts if not h]
> That's what I call nice and concise, and at least for me the meaning is clear 
> by just looking at the code.

I suspect it's only clear to you because you wrote it.
I would find that piece of code *extremely* confusing --
it looks like it's going to return a list full of Nones!

If, on the other hand, it were written something like

   ping_hosts = [h for h in hosts if h.expired()]

the meaning would be crystal clear to everyone, I think.

Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand

More information about the Python-list mailing list