design question: generator object with other attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Isaac

    #1

    design question: generator object with other attributes

    Essentially I want a generator that I can query about
    its characteristics . (E.g., a random number generator
    that I want to be able to ask about is distributional
    parameters.)

    I am thinking of a class that wraps a generator.
    An object of this class will have a ``next`` method that simply
    returns the value the object get by calling the wrapped
    generator.

    A reasonable approach?

    Thanks,
    Alan Isaac

    PS Here is a useless class to illustrate the basic idea
    that you could have both attribute access and a
    generator-connected ``next`` method.

    class Start2Stop:
    def __init(start,st op):
    self.start = start
    self.stop = stop
    self.numgen = (i for in in xrange(start,st op))
    def next(self):
    return self.numgen.nex t()



  • Robert Kern

    #2
    Re: design question: generator object with other attributes

    Alan Isaac wrote:
    Essentially I want a generator that I can query about
    its characteristics . (E.g., a random number generator
    that I want to be able to ask about is distributional
    parameters.)
    >
    I am thinking of a class that wraps a generator.
    An object of this class will have a ``next`` method that simply
    returns the value the object get by calling the wrapped
    generator.
    You will probably also need to define

    def __iter__(self):
    return self

    if I remember correctly.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • James Stroud

      #3
      Re: design question: generator object with other attributes

      Alan Isaac wrote:
      Essentially I want a generator that I can query about
      its characteristics . (E.g., a random number generator
      that I want to be able to ask about is distributional
      parameters.)
      >
      I am thinking of a class that wraps a generator.
      An object of this class will have a ``next`` method that simply
      returns the value the object get by calling the wrapped
      generator.
      >
      A reasonable approach?
      >
      Thanks,
      Alan Isaac
      >
      PS Here is a useless class to illustrate the basic idea
      that you could have both attribute access and a
      generator-connected ``next`` method.
      >
      class Start2Stop:
      def __init(start,st op):
      self.start = start
      self.stop = stop
      self.numgen = (i for in in xrange(start,st op))
      def next(self):
      return self.numgen.nex t()
      >
      >
      >
      In case you are having problems, "__init(start,s top)" should be spelled
      "__init__(s elf, start, stop)".

      James

      Comment

      • Alex Martelli

        #4
        Re: design question: generator object with other attributes

        Alan Isaac <aisaac@america n.eduwrote:
        Essentially I want a generator that I can query about
        its characteristics . (E.g., a random number generator
        that I want to be able to ask about is distributional
        parameters.)
        >
        I am thinking of a class that wraps a generator.
        An object of this class will have a ``next`` method that simply
        returns the value the object get by calling the wrapped
        generator.
        >
        A reasonable approach?
        Sure, given the undiscussed constraints.
        >
        Thanks,
        Alan Isaac
        >
        PS Here is a useless class to illustrate the basic idea
        that you could have both attribute access and a
        generator-connected ``next`` method.
        >
        class Start2Stop:
        def __init(start,st op):
        self.start = start
        self.stop = stop
        self.numgen = (i for in in xrange(start,st op))
        def next(self):
        return self.numgen.nex t()
        Don't forget to add:

        def __iter__(self): return self


        Alex.

        Comment

        Working...