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

2008-03-03 Thread Marc 'BlackJack' Rintsch
On Mon, 03 Mar 2008 12:17:39 +0100, M.-A. Lemburg wrote:

> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2008-03-03 Thread M.-A. Lemburg
On 2008-03-02 15:06, Preben Randhol wrote:
> 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)
 >>> Python/Zope Consulting and Support ...http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2008-03-02 Thread Preben Randhol
On Sun, 2 Mar 2008 06:15:54 -0800 (PST)
Giles Brown <[EMAIL PROTECTED]> wrote:

> http://docs.python.org/lib/typeiter.html

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2008-03-02 Thread Preben Randhol
On Sun, 2 Mar 2008 08:09:24 -0800 (PST)
[EMAIL PROTECTED] wrote:

> On Mar 2, 8:15 am, Giles Brown <[EMAIL PROTECTED]> wrote:

> > http://docs.python.org/lib/typeiter.html
> 
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list

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

2008-03-02 Thread castironpi
On Mar 2, 8:15 am, Giles Brown <[EMAIL PROTECTED]> wrote:
> On Mar 2, 2:08 pm, Preben Randhol 
> [EMAIL PROTECTED]> wrote:
> > On Sun, 2 Mar 2008 15:06:17 +0100
>
> > Preben Randhol <[EMAIL PROTECTED]> wrote:
> > >    class dbase(list):
>
> > Sorry the definition of the class is:
>
> >         class dbase(object):
>
> > it doesn't derive from the list class.
>
> > Preben
>
> http://docs.python.org/lib/typeiter.html

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.

You can also use:

>>> a= [2,3,4]
>>> b= iter( a )
>>> next( b )
2
>>> next( b )
3
>>> next( b )
4
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2008-03-02 Thread Giles Brown
On Mar 2, 2:08 pm, Preben Randhol  wrote:
> On Sun, 2 Mar 2008 15:06:17 +0100
>
> Preben Randhol <[EMAIL PROTECTED]> wrote:
> >class dbase(list):
>
> Sorry the definition of the class is:
>
> class dbase(object):
>
> it doesn't derive from the list class.
>
> Preben

http://docs.python.org/lib/typeiter.html
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2008-03-02 Thread Preben Randhol
On Sun, 2 Mar 2008 15:06:17 +0100
Preben Randhol <[EMAIL PROTECTED]> wrote:

>   class dbase(list):

Sorry the definition of the class is:

class dbase(object):

it doesn't derive from the list class.

Preben
-- 
http://mail.python.org/mailman/listinfo/python-list