[Python-Dev] Subtyping mutable and immutable built-in types (original) (raw)

Edward C. Jones edcjones at erols.com
Thu Apr 29 00:09:41 EDT 2004


#! /usr/bin/env python

According to

http://www.cafepy.com/articles/python_attributes_and_methods/ch03s02.html

the correct ways to subtype mutable and immutable built-in types are

Mutable

class MyList(list): def init(self, itr): list.init(self, [int(x) for x in itr])

Immutable

class MyTuple(tuple): def new(typ, itr): return tuple.new(typ, [int(x) for x in itr])

This doesn't work.

class MyTuple(tuple): def init(self, itr): tuple.init(self, [int(x) for x in itr])

A variant is

class MyList(list): def init(self, *args): list.init(self, args)

class MyTuple(tuple): def new(typ, *args): return tuple.new(typ, args)

This doesn't work.

class MyTuple2(tuple): def init(self, *args): tuple.init(self, args)

print MyList(1, 2) print MyTuple(1, 2) print MyTuple2(1, 2)

This warty stuff needs documenting.



More information about the Python-Dev mailing list