[Python-Dev] Q: why doesn't list.extend use the sequence interface?
Fred L. Drake, Jr.
fdrake@beopen.com
Fri, 16 Jun 2000 15:42:12 -0400 (EDT)
Barry A. Warsaw writes:
> Jeremy's right that this can get hairy if your as anal as I am about
> error checking and refcounting. In addition to PySequence_Length()
> ability to lie, there are all sorts of errors that can happen, which
> should abort the whole process and clean up any temporary objects.
> Plus you've now got three very similar branches in the code, which
> adds its own overhead.
>
> So you should decide whether this situation will be used widely enough
> to warrant the extra coding complexity for the performance win.
> list.extend() is probably not used too often these days, but it should
> be 'cause it's cool. So maybe it's worth it in this case.
>
> On the other hand, this is a situation that comes up often, and if we
> were to really audit the APIs, we'd probably find even more cases
> where restrictions to concrete lists or tuples should be relaxed to
> accept any sequence. Maybe there's a way we can factor this out in a
> single function which handles this once and for all?
How about this: special case the list & tuple flavors (since that'll
be roughly what's in there now), and for other sequences, convert to
either a tuple or a list and let it pass through that special case,
keeping a flag to discard the temporary list when done?
There are two APIs, PySequence_List() and PySequence_Tuple(), which
can take care of a lot of the machinery in dealing with sequences that
lie about their length or otherwise break (during __getitem__() calls
or whatever), that a large part of the work is done.
-Fred
--
Fred L. Drake, Jr. <fdrake at beopen.com>
BeOpen PythonLabs Team Member