[Python-Dev] slice subscripts for sequences and mappings

Thomas Wouters thomas at python.org
Sun Mar 4 03:24:40 CET 2012


On Sat, Mar 3, 2012 at 13:12, Stefan Behnel <stefan_ml at behnel.de> wrote:

> Thomas Wouters, 03.03.2012 21:59:
> > Why even have separate tp_as_sequence and tp_as_mapping anymore? That
> > particular distinction never existed for Python types, so why should it
> > exist for C types at all? I forget if there was ever a real point to it,
> > but all it seems to do now is create confusion, what with many sequence
> > types implementing both, and PyMapping_Check() and PySequence_Check()
> doing
> > seemingly random things to come up with somewhat sensible answers. Do
> note
> > that the dict type actually implements tp_as_sequence (in order to
> support
> > containtment tests) and that PySequence_Check() has to explicitly return
> 0
> > for dicts -- which means that it will give the "wrong" answer for another
> > type that behaves exactly like dicts.
> >
> > Getting rid of the misleading distinction seems like a much better idea
> > than trying to re-conflate some of the issues.
>
> We're too far away from the release of Python 4 to change something with
> that kind of impact, though.


It's not hard to do this in a backward-compatible way. Either grow one of
the tp_as_* to include everything a 'unified' tp_as_everything struct would
need, or add a new tp_as_everything slot in the type struct. Then add a
tp_flag to indicate that the type has this new layout/slot and guard all
uses of the new slots with a check for that flag. If the type doesn't have
the new layout or doesn't have it or the slots in it set, the code can fall
back to the old try-one-and-then-the-other behaviour of dealing with
tp_as_sequence and tp_as_mapping.

(Let's not forget about tp_as_sequence.sq_concat, tp_as_number.nb_add,
tp_as_sequence.sq_repeat and tp_as_number.nb_mul either.)

-- 
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120303/468b5f2d/attachment-0001.html>


More information about the Python-Dev mailing list