D
David Mertz, Ph.D.
|[email protected] (David Mertz, Ph.D.) wrote:
|> >>> def echo():
|> ... message = [None]
|> ... while message[0] != "EXIT":
|> ... yield message
|> ...
|> >>> for mess in echo():
|> ... if mess[0] is not None: print mess[0]
|> ... mess[0] = raw_input("Word: ")
(e-mail address removed) (Michele Simionato) wrote previously:
|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.
|class MyIterator(Iterator):
| def __gen__(self):
| self.x=1
| yield self.x # will be changed outside the class
| yield self.x
|
|iterator=MyIterator()
|print iterator.next()
|iterator.x=5
|print iterator.next()
I don't disagree, of course, with Michele's class-based approach. For
something fleshed out, his style lets you do a lot more with the
underlying class.
But there *is* something awfully elegant about the function-like
definition syntax of simple generators. Doing what I do in the simple
example--yielding a mutable object, and manipulating that object outside
the generator--feels very Pythonic to me. I use a list for this, but a
different mutable object would work similarly (a dictionary, instance,
shelve, etc.)
But obviously, keep both styles in mind; either might be a good
solution to a problem. It just depends on your needs and coding style.
Yours, David...
--
mertz@ | The specter of free information is haunting the `Net! All the
gnosis | powers of IP- and crypto-tyranny have entered into an unholy
..cx | alliance...ideas have nothing to lose but their chains. Unite
| against "intellectual property" and anti-privacy regimes!
-------------------------------------------------------------------------
|> >>> def echo():
|> ... message = [None]
|> ... while message[0] != "EXIT":
|> ... yield message
|> ...
|> >>> for mess in echo():
|> ... if mess[0] is not None: print mess[0]
|> ... mess[0] = raw_input("Word: ")
(e-mail address removed) (Michele Simionato) wrote previously:
|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.
|class MyIterator(Iterator):
| def __gen__(self):
| self.x=1
| yield self.x # will be changed outside the class
| yield self.x
|
|iterator=MyIterator()
|print iterator.next()
|iterator.x=5
|print iterator.next()
I don't disagree, of course, with Michele's class-based approach. For
something fleshed out, his style lets you do a lot more with the
underlying class.
But there *is* something awfully elegant about the function-like
definition syntax of simple generators. Doing what I do in the simple
example--yielding a mutable object, and manipulating that object outside
the generator--feels very Pythonic to me. I use a list for this, but a
different mutable object would work similarly (a dictionary, instance,
shelve, etc.)
But obviously, keep both styles in mind; either might be a good
solution to a problem. It just depends on your needs and coding style.
Yours, David...
--
mertz@ | The specter of free information is haunting the `Net! All the
gnosis | powers of IP- and crypto-tyranny have entered into an unholy
..cx | alliance...ideas have nothing to lose but their chains. Unite
| against "intellectual property" and anti-privacy regimes!
-------------------------------------------------------------------------