[Tutor] custom types question

Thomas Clive Richards thomi at imail.net.nz
Wed Feb 11 23:45:32 EST 2004

Hi guys,

I'm building an application which uses both 2D points, and row,col indexes a 
lot. It would be really cool if I could create a few custom types, and then 
create operators for them.

For example, a 2D point could be represented by a tuple, or list:

>>> [23,48]

However, I frequently need to add or subtract these points. If I try this 
as-is, I get this:

>>> [10,10] + [23,20]
[10, 10, 23, 20]

What I'd like to be able to do, is something like this:

>>> [10,10] + [10,12]
>>>[10,10] - [20,25]

I believe you do this by subclassing existing types, am I correct? I tried 
>>> class point2D(list):
...     def __init__(self,X,Y):
...             self.x = X
...             self.y = Y
>>> point2D(10,10)

A few problems with this... It doesn't actually give me any additional 
functionality.. I still can't add points...

I'm sure I've seen soem documentation somewhere about how to do this. I guess 
I should start bookmarking interesting pages..

Anyway, any pointers would be very helpful, thanks ;)


Thomi Richards,
thomi at once.net.nz

More information about the Tutor mailing list