[Tutor] custom types question

Michael Janssen Janssen at rz.uni-frankfurt.de
Thu Feb 12 05:41:10 EST 2004


On Thu, 12 Feb 2004, Thomas Clive Richards wrote:

> class point2D(list):
> 	def __init__(self,x,y):
> 		self.__data = [x,y]

You derives your class from "list", which results in "self" being a
list-like object (for all I've understand from new-style classes...).

Then you attach an attribute "__data" on self with the result of having
two lists objects: self and self.__data .

You should decide to either derive from list and work on self from then
on or you should derive from nothing, do "self.__data = [x,y]" and work
on __data from then on (Since you don't want to reimplement all
list-methods on your own you should derive from UserList and overwrite
__add__ and __sub__ ).


[Hmm, enough hints? Perhaps a last one: when you want the
new-style-derive-from-object solution you have to understand why
you can't simply overwrite the __init__ method. You must use the
builtin function super]


Michael




More information about the Tutor mailing list