Re: Conditionally implementing __iter__ in new style classes

2005-07-07 Thread Bengt Richter
On Thu, 07 Jul 2005 22:04:31 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: >[EMAIL PROTECTED] (Bengt Richter) writes: > >> On Thu, 07 Jul 2005 09:51:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: >> >>>[EMAIL PROTECTED] (Bengt Richter) writes: >>> On Wed, 06 Jul 2005 17:57:42 +0200, Tho

Re: Conditionally implementing __iter__ in new style classes

2005-07-07 Thread Thomas Heller
[EMAIL PROTECTED] (Bengt Richter) writes: > On Thu, 07 Jul 2005 09:51:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: > >>[EMAIL PROTECTED] (Bengt Richter) writes: >> >>> On Wed, 06 Jul 2005 17:57:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: >>> I'm trying to implement __iter__ on an

Re: Conditionally implementing __iter__ in new style classes

2005-07-07 Thread Dieter Maurer
Thomas Heller <[EMAIL PROTECTED]> writes on Wed, 06 Jul 2005 18:07:10 +0200: > Thomas Heller <[EMAIL PROTECTED]> writes: > ... > > class Base: > > def __getattr__(self, name): > > if name == "__iter__" and hasattr(self, "Iterator"): > > return self.Iterator > > raise

Re: Conditionally implementing __iter__ in new style classes

2005-07-07 Thread Bengt Richter
On Thu, 07 Jul 2005 09:51:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: >[EMAIL PROTECTED] (Bengt Richter) writes: > >> On Wed, 06 Jul 2005 17:57:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: >> >>>I'm trying to implement __iter__ on an abstract base class while I don't >>>know whether s

Re: Conditionally implementing __iter__ in new style classes

2005-07-07 Thread Thomas Heller
[EMAIL PROTECTED] (Bengt Richter) writes: > On Wed, 06 Jul 2005 17:57:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: > >>I'm trying to implement __iter__ on an abstract base class while I don't >>know whether subclasses support that or not. > Will a property or custom descriptor do what you w

RE: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread Delaney, Timothy (Tim)
Thomas Heller wrote: > I forgot to mention this: The Base class also implements a __getitem__ > method which should be used for iteration if the .Iterator method in > the subclass is not available. So it seems impossible to raise an > exception in the __iter__ method if .Iterator is not found - _

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread Bengt Richter
On Wed, 06 Jul 2005 17:57:42 +0200, Thomas Heller <[EMAIL PROTECTED]> wrote: >I'm trying to implement __iter__ on an abstract base class while I don't >know whether subclasses support that or not. >Hope that makes sense, if not, this code should be clearer: > >class Base: >def __getattr__(self

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread Peter Otten
Thomas Heller wrote: > I'm trying to implement __iter__ on an abstract base class while I don't > know whether subclasses support that or not. > Hope that makes sense, if not, this code should be clearer: > > class Base: > def __getattr__(self, name): > if name == "__iter__" and hasat

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread Leif K-Brooks
Thomas Heller wrote: > I forgot to mention this: The Base class also implements a __getitem__ > method which should be used for iteration if the .Iterator method in the > subclass is not available. So it seems impossible to raise an exception > in the __iter__ method if .Iterator is not found - __

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread infidel
Something like this: >>> class Base(object): ... def __getitem__(self, key): ... return key ... def __iter__(self): ... yield self[1] ... yield self['foo'] ... yield self[3.0] ... >>> class ConcreteIterable(Base): ... def __iter__(self):

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread infidel
Why not define an Iterator method in your Base class that does the iteration using __getitem__, and any subclass that wants to do something else just defines its own Iterator method? For that matter, you could just use the __iter__ methods of Base and Concrete instead of a separate method. -- ht

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread infidel
I'm not sure I understand why you would want to. Just don't define __iter__ on your newstyle class and you'll get the expected behavior. -- http://mail.python.org/mailman/listinfo/python-list

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread harold fellermann
> I'm trying to implement __iter__ on an abstract base class while I > don't > know whether subclasses support that or not. > Hope that makes sense, if not, this code should be clearer: > > class Base: > def __getattr__(self, name): > if name == "__iter__" and hasattr(self, "Iterator")

Re: Conditionally implementing __iter__ in new style classes

2005-07-06 Thread Thomas Heller
Thomas Heller <[EMAIL PROTECTED]> writes: > I'm trying to implement __iter__ on an abstract base class while I don't > know whether subclasses support that or not. > Hope that makes sense, if not, this code should be clearer: > > class Base: > def __getattr__(self, name): > if name ==

Conditionally implementing __iter__ in new style classes

2005-07-06 Thread Thomas Heller
I'm trying to implement __iter__ on an abstract base class while I don't know whether subclasses support that or not. Hope that makes sense, if not, this code should be clearer: class Base: def __getattr__(self, name): if name == "__iter__" and hasattr(self, "Iterator"): re