Coding style

Carl Banks pavlovevidence at gmail.com
Tue Jul 18 22:35:23 EDT 2006


Jorge Godoy wrote:
> "Carl Banks" <pavlovevidence at gmail.com> writes:
>
> > Well, I certainly can agree with that, except for the last point. :)  I
> > certainly wouldn't want to keep that unfortunate behavior around just I
> > have something to use as an argument using len to test emptiness.
>
> On the other hand, having this behavior makes it so simple to deal with some
> algorithms because on doesn't need to know if the given object does or does
> not support len()...

First of all, the unfortunate behavior I was referring to was the fact
that iterables that have unknown length are true in a boolean context,
even if they happen to be empty.  Bruno and I had a disagreement over
whether this should also be a wart for lists and such, but I think we
agree that it would be better generators and other iterators raised an
exception in a boolean context.

Getting back to your reply--can you give an example of a useful
function that needs to do what you want?  (Let's disregard iterators
and arrays for now, and focus on older Python types like lists, tuples,
strings, integers, floats, None, etc.)  Can you give me an example of a
useful function that needs to test the truth value of an object, but
that can't rely on it having a length?

The only example I can think of here is a function that does nothing
with an object except pass it to other functions.  Almost anything else
you do with an object pigeonholes it as a sequence or atomic type:
you've already assumed whether it has a length or not.


Carl Banks




More information about the Python-list mailing list