[Python-Dev] new datatypes - should they subclasses (deque(list)?)

Jewett, Jim J jim.jewett at eds.com
Thu Apr 29 14:28:26 EDT 2004


The collections module will be offering 
new datatypes, including a double-ended
queue (deque).

Right now, these are new elemental types,
inheriting directly from object.  I believe
they should be subclassed from existing 
classes where this makes sense.  Even if
no actual methods are inherited, the API
is valuable.

For instance, should:

	list([1,2,3]) == deque([1,2,3])
	deque([1]) < list([1,2]) < deque([3])

If deque inherits directly from object,
then the only comparison available is
based on address.  If deque subclasses
list (or at least pretends to), then
sensible comparisons can be made.

All list operations make sense on a deque
(and vice versa).  Operations may be more 
(or less) efficient depending on the type, 
but they do make sense.  Even if the concern 
is to avoid encouraging inefficient code, 
the methods should still be available.

	d = deque(...)
	d[4:11] = ('x', 'y')

is inefficient, but still better than

	d = deque(...)
	temp = list(d)
	temp[4:11] = ('x', 'y')
	d = deque(temp)

As an additional benefit, making deque a 
subclass of list with the same API gives
later python implementations more freedom;
in theory, the compiler could determine
which internal representation made more
sense for each particular case.

-jJ



More information about the Python-Dev mailing list