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

P

Preben Randhol

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
 
P

Preben Randhol

On Sun, 2 Mar 2008 08:09:24 -0800 (PST)
Be careful on your descision to return an ordered iterator or not--
that is, whether it iterates over the dictionary or the list (if I
understand you correctly). If the order's unimportant then please
disregard.

I was thinking to iterate over the list which contains the uniq_ids as
order is important :)

Thanks!

Preben
 
M

M.-A. Lemburg

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.

The easiest is to implement an iterator which then get's
returned by the .__iter__() method.

http://www.python.org/doc/lib/typeiter.html

It's also possible to implement .__getitem__() and .__len__()
methods and have Python create an iterator on-the-fly. That's
how Python used to work before iterators were added to the
language.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Mar 03 2008)________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
 
M

Marc 'BlackJack' Rintsch

It's also possible to implement .__getitem__() and .__len__()
methods and have Python create an iterator on-the-fly. That's
how Python used to work before iterators were added to the
language.

A suitable `__getitem__()` is enough. The end will be signaled by an
`IndexError`.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top