list() coercion

Ian Bicking ianb at colorstudy.com
Wed Jul 16 20:11:13 EDT 2003


I have an iterable object.  It supports many list-like methods,
specifically __len__.  These methods are rather expensive (they result
in database calls, COUNT(*) to be specific), but cheaper than iterating
over the object.  Sometimes it is useful to create a list from the
iterator, using list().  However, list() seems to call the object's
__len__, I imagine to pre-allocate space.  This is a problem, as
pre-allocation saves much less than is spent doing __len__.

Is there a way I can keep this from happening?  Maybe something list()
tries first that I can make fail.  (I notice list() catches any
exceptions in __len__ and then will just skip that step)

  Ian







More information about the Python-list mailing list