[Tutor] custom types question

Thomas Clive Richards thomi at imail.net.nz
Thu Feb 12 02:04:07 EST 2004


Hehe..

never happy with what I've got, I'd like to try something else..

with other types, you can create them by using certain symbols. For example, 
to create a string, you'd do something like:

>>> s = 'this is a string'

the "'" symbols identify that the characters between them are to be treated as 
a string, correct?

dictionaries use the '{' symbol, lists use '[', tuples use '(' etc. etc.

Is there any easy way to bind a symbol to a custom type? so far, teh code for 
my 2D point type is as follows:

class point2D(list):
	def __init__(self,x,y):
		self.__data = [x,y]
	def __repr__(self):
		return repr(self.__data)
	def __add__(self,other):
		temp = other[:]
		temp[0] += self.__data[0]
		temp[1] += self.__data[1]
		return self.__class__(temp[0], temp[1])
	def __sub__(self,other):
		temp = other[:]
		temp[0] -= self.__data[0]
		temp[1] -= self.__data[1]
		return self.__class__(temp[0],temp[1])
	def __nonzero__(self):
		if self.__data == [0,0]:
			return False
		else:
			return True

I'm guessing that this isn't really possible, without editing the python 
source code itself..

but still, it'd be pretty cool...

Also, does anyone know if there are any builtin python types which are written 
in python, or are they all in C? It'd be good to be able to look through the 
source code to the builtin types and see how they're done. I guess they'll be 
in C for the extra speed though.

Anyway, thanks ;)


-- 

Thomi Richards,
thomi at once.net.nz




More information about the Tutor mailing list