list() coercion

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed Jul 16 20:42:46 EDT 2003


> From: Ian Bicking [mailto:ianb at colorstudy.com]
> 
> 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)

Simplest thing is probably:

ll = MyListLikeObject()
li = iter(ll)
l = list(li)

i.e. explicitly create an iterator (which doesn't have a __len__) and create the list from that.

OTOH, if the problem is that creating the iterator is causing the problem (calling __len__), you may need to create a proxy object that doesn't have a __len__ and call list() on that.

Tim Delaney





More information about the Python-list mailing list