michele.simionato at poste.it
Fri Dec 12 07:44:15 CET 2003
mertz at gnosis.cx (David Mertz, Ph.D.) wrote in message news:<mailman.85.1071171394.9307.python-list at python.org>...
> But the state need not be global, just a mutable object yielded by a
> generator. As I thought about this fact, I have come to find Raymond
> Hettinger's proposals for enhancing simple generators less urgent (but I
> probably still vote +1, though now moot).
> >>> def echo():
> ... message = [None]
> ... while message != "EXIT":
> ... yield message
> >>> for mess in echo():
> ... if mess is not None: print mess
> ... mess = raw_input("Word: ")
> Word: foo
> Word: bar
> Word: EXIT
> This is a toy example, but the point is that we are perfectly able to
> pass data back into a generator without using global state.
A more verbose but arguably more elegant way would be to wrap the
generator in a class. Let me repost some code I wrote some time ago.
"""An object-oriented interface to iterators-generators"""
"""__gen__ is automatically called by __init__, so must have signature
compatibile with __init__. Subclasses should not need to override __init__:
you can do it, but you must do it cooperatively or, at least, ensure that
__gen__ is called correctly and its value assigned to self.iterator.
"Trivial generator, to be overridden in subclasses"
yield self.x # will be changed outside the class
Wrapping the generator in the class, I can pass parameters to it (in
this case x). IOW, here the generator has an explicit "self" rather
than an implicit "__self__" as in the PEP. I am not sure if I like the
PEP, wouldn't be easier to have a built-in iterator class?
More information about the Python-list