[Fwd: while row = c.fetchone(): # syntax error???]
Alex Martelli
alex at magenta.com
Fri Aug 11 17:20:39 EDT 2000
"Bernhard Herzog" <herzog at online.de> wrote in message
news:m3lmy3hk0m.fsf at greebo.nodomain.de...
> "Alex Martelli" <alex at magenta.com> writes:
>
> > I've already mentioned one
> > class-based alternative, but another is:
> >
> > for row in iterator(c.fetchone):
> > doit(row)
> >
> > with, obviously, hidden somewhere in a myutil.py...:
> >
> > class iterator:
> > def __init__(self,iter):
> > self.iter=(iter,)
> > def __getitem__(self,index):
> > nextone=self.iter[0]()
> > if not nextone:
> > raise IndexError
> > return nextone
>
> Why do you put iter into a tuple? Because functions are converted into
> methods sometimes? That shouldn't happen in this case, because iter is
> an instance variable. The conversion only happens for class attributes.
True. It's just a habit I've tried to acquire when setting callable
objects as attributes of whatever -- just to avoid mysterious bugs
down the line. Much like writing "if(0==x)" in C rather than the
other way 'round -- preventive-funny-idiom syndrome.
Alex
More information about the Python-list
mailing list