Alternative iterator syntax

Rainer Deyke root at rainerdeyke.com
Wed Feb 21 15:29:21 EST 2001


"Tim Hochberg" <tim.hochberg at ieee.org> wrote in message
news:MBUk6.126$a_2.14850 at news1.rdc1.az.home.com...
> "Michael Hudson" <mwh21 at cam.ac.uk> wrote in message
> news:m37l2kszu7.fsf at atrus.jesus.cam.ac.uk...
> > aahz at panix.com (Aahz Maruch) writes:
> > class _Keys:
> >    def __init__(self, dict):
> >        self.dict = dict
> >    def __contains__(self, item):
> >        return self.dict.has_key(item)
> >    def __call__(self):
> >        return self.dict.keys()
> >    ... etc ...
> >
> > class UserDict:
> >     def __init__(self, dict=None):
> >         if data is None:
> >             data = {}
> >         self.data = dict
> >         self.keys = _Keys(dict)
> >         ... etc ...

> I don't think this will work right though. Consider:
>
> for k in aUserDict.keys:
>    # Do some stuff
> for k in aUserDict.keys:
>     # Do some more stuff

UserDict.__getattr__ could construct a new 'keys' object on demand instead.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list