design question: generator object with other attributes
Alex Martelli
aleax at mac.com
Fri Mar 23 01:11:17 EDT 2007
Alan Isaac <aisaac at american.edu> 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?
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,stop):
> self.start = start
> self.stop = stop
> self.numgen = (i for in in xrange(start,stop))
> def next(self):
> return self.numgen.next()
Don't forget to add:
def __iter__(self): return self
Alex.
More information about the Python-list
mailing list