[Python-Dev] __contains__ hook

M.-A. Lemburg mal@lemburg.com
Thu, 03 Feb 2000 00:33:17 +0100


Guido van Rossum wrote:
> 
> > > But the real issue is what Moshe himself already brings up: contains
> > > should have a slot in the type struct, so extension types can also
> > > define this.
> > >
> > > Moshe, do you feel like doing this right?
> >
> > Yes, but not in the near future. Wouldn't adding a new slot break old
> > extension types? I'm a bit ignorant on the subject
> 
> There are some spare slots in PyTypeObject:
> 
>         /* More spares */
>         long tp_xxx5;
>         long tp_xxx6;
>         long tp_xxx7;
>         long tp_xxx8;
> 
> These can be used for binary compatibility; old extensions will simply
> not have the new feature.
> 
> There's also a more sophisticated feature, implemented through
> tp_flags, which can indicate that an extension is aware of a
> particular feature.  These comments in object.h may explain this:
> 
> /*
> 
> Type flags (tp_flags)

Shouldn't 'in' be a slot of the sequence methods ? I'd suggest
creating a new tp_flag bit and then extending tp_as_sequence
with:

	binaryfunc sq_contains;

plus of course add an abstract function to abstract.c:

	PySequence_Contain(PyObject *container, PyObject *element)

which uses the above slot after testing the tp_flag setting.
Python instances, lists, tuples should then support this new
slot. We could even sneak in support for dictionaries once we
decide whether semantics whould be 
	1. key in dict
	2. value in dict
    or  3. (key,value) in dict

:-)

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/