Can one get "for x in y" to work for non builtin classes?

Preben Randhol randhol+valid_for_reply_from_news at pvv.org
Sun Mar 2 09:06:17 EST 2008


Hi

I'm making a kind of ordered dictionary class. It is not exactly a
dictionary, but it uses a list and dictionary to store the data. 

Something like:

	class dbase(list):
	'''Database class keeping track of the order and data'''

	def __init__(self):
		self.__data = {}
		self.__order = []
		self.__uniq_id = 0

I'm just wondering if it is possible to get my class to work so that if
one do:


	d=dbase()
	d.append("Data")
	d.append([1,2])
	
one can do like this to iterate over the data.

	for x in d:
		...

I'm looking at the list class but I don't quite understand from pydoc
which __ __ methods I have to implement to get the above to work.

Thanks in advance

Preben



More information about the Python-list mailing list