trouble with generators

Hans-Peter Jansen hpj at urpla.net
Thu May 10 14:09:06 EDT 2007


Marc 'BlackJack' Rintsch wrote:

> In <f1v4in$4qj$01$1 at news.t-online.com>, Hans-Peter Jansen wrote:
> 
>> class Gen(object):
>>     def records(self, cls):
>>         for i in range(3):
>>             setattr(cls, "id", "%s%s" % (cls.__doc__,  i))
>>             yield cls
>>
>> […]
>>
>> class GenA(Gen):
>>     def __init__(self):
>>         self.genB = GenB()
>>     
>>     def records(self):
>>         for a in Gen.records(self, A()):
> 
> Here you create an instance of `A` and pass that *instance* and not the
> *class*.  If  you would pass the class here, you must create objects in
> `Gen.records()`.

Yes, that was my fault, as you both found.
 
> Ciao,
> Marc 'BlackJack' Rintsch

Thanks, Marc.

Cheers, Pete



More information about the Python-list mailing list