PEP 288: Generator Attributes

Denis S. Otkidach ods at strana.ru
Wed Dec 4 04:36:38 EST 2002


On Wed, 4 Dec 2002, Raymond Hettinger wrote:

RH> So, the behavior of mygen() is radically different depending
RH> on whether it
RH> is called with iter(mygen()) or mygen.iter().    It looks
RH> like the parameter
RH> string is being asked to pull double duty.  How would you
RH> code an equivalent
RH> to:
RH>
RH> def logger(afile, author):
RH>     while True:
RH>         print >> file, __self__.data, ':changed by', author
RH>         yield None

class logger(object):

    def __init__(self, afile, author):
        self.afile = afile
        self.author = author
        self.data = 'default data'
        self.next = self._gen().next

    def _gen(self):
        while True:
            print >> self.afile, self.data, ':changed by', self.author
            yield None


-- 
Denis S. Otkidach
http://www.python.ru/      [ru]





More information about the Python-list mailing list