[Python-Dev] PEP 288: Generator Attributes
Jack Jansen
Jack.Jansen@cwi.nl
Fri, 22 Nov 2002 11:35:28 +0100
On Friday, Nov 22, 2002, at 09:46 Europe/Amsterdam, Raymond Hettinger
wrote:
> def outputCaps(logfile):
> while True:
> line = __self__.data
> logfile.write(line.upper)
> yield None
> outputCaps.data = "" # optional attribute initialization
>
> g = outputCaps(open('logfil.txt','w'))
> for line in open('myfile.txt'):
> g.data = line
> g.next()
I don't like it, there's "Magic! Magic!" written all over it.
Generators have always given me that feeling (you start reading them as
a function, then 20 lines down you meet a "yield" and suddenly realize
you have to start reading at the top again, keeping in mind that this
is a persistent stack frame), but with the __self__ plus the fact that
you local variables may not be what they appear to be makes it hairy.
You basically cannot understand the code without knowing the code of
the caller. There's also absolutely no way to get encapsulation. So
count me in for a -1.
Generators have to me always felt more "class-instance-like" than
"function-like", and I guess this just goes to show it.
--
- Jack Jansen <Jack.Jansen@oratrix.com>
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma
Goldman -