Generator question

Emile van Sebille emile at fenx.com
Wed Dec 22 18:46:37 EST 2010


On 12/22/2010 3:15 PM Victor Eijkhout said...
> So I have a generator, either as a free function or in a class and I
> want to generate objects that are initialized from the generated things.
>
> def generator():
>          for whatever:
>                  yield something
> class Object():
>          def __init__(self):

How about change to

           def __init__(self, data=generator()):

>                  self.data = # the next thing from generator
>

then...

                   self.data = data.next()# the next thing from generator

HTH,

Emile



> I have not been able to implement this elegantly. For external reasons
> the following syntax is unacceptable:
>
> for g in generator():
>          ob = Object(g)
>
> I really want to be able to write "Object()" in any location and get a
> properly initialized object.
>
> Hints appreciated.
>
> Victor.





More information about the Python-list mailing list