Generator question

Discussion in 'Python' started by Victor Eijkhout, Dec 22, 2010.

  1. 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):
    self.data = # the next thing from generator

    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.
    --
    Victor Eijkhout -- eijkhout at tacc utexas edu
     
    Victor Eijkhout, Dec 22, 2010
    #1
    1. Advertising

  2. 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.
     
    Emile van Sebille, Dec 22, 2010
    #2
    1. Advertising

  3. On Wed, Dec 22, 2010 at 3:15 PM, Victor Eijkhout <> wrote:
    > 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):
    >                self.data = # the next thing from generator
    >
    > 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.
    > --
    > Victor Eijkhout -- eijkhout at tacc utexas edu
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    You likely want a class variable:

    #!/usr/bin/python

    def generator():
    i = 0
    while True:
    yield i
    i += 1

    class Object:
    gen = generator()

    def __init__(self):
    self.data = Object.gen.next()

    def __str__(self):
    return str(self.data)

    o1 = Object()
    o2 = Object()
    o3 = Object()

    print o1
    print o2
    print o3
     
    Dan Stromberg, Dec 22, 2010
    #3
  4. Dan Stromberg <> wrote:

    > You likely want a class variable:


    Sounds like an elegant solution. Thanks!

    Victor.
    --
    Victor Eijkhout -- eijkhout at tacc utexas edu
     
    Victor Eijkhout, Dec 23, 2010
    #4
  5. Victor Eijkhout

    Nobody Guest

    On Wed, 22 Dec 2010 15:49:31 -0800, Dan Stromberg wrote:

    > def generator():
    > i = 0
    > while True:
    > yield i
    > i += 1


    Shorter version:

    from itertools import count as generator
     
    Nobody, Dec 23, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    5,095
    Peter
    Apr 19, 2006
  2. TheDustbustr
    Replies:
    1
    Views:
    496
    Sami Hangaslammi
    Jul 25, 2003
  3. Replies:
    9
    Views:
    610
  4. Chris Withers

    Problems with email.Generator.Generator

    Chris Withers, Sep 11, 2006, in forum: Python
    Replies:
    20
    Views:
    1,761
    Max M
    Sep 12, 2006
  5. Terry Reedy

    Generator functions subclass generator?

    Terry Reedy, Jun 18, 2009, in forum: Python
    Replies:
    0
    Views:
    502
    Terry Reedy
    Jun 18, 2009
Loading...

Share This Page