Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85210
| From | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| Subject | basic generator question |
| Date | 2015-02-04 08:23 -0500 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18458.1423056240.18130.python-list@python.org> (permalink) |
I have an object that expects to call a callable to get a value:
class obj:
def __init__ (self, gen):
self.gen = gen
def __call__ (self):
return self.gen()
Now I want gen to be a callable that repeats N times. I'm thinking, this
sounds perfect for yield
class rpt:
def __init__ (self, value, rpt):
self.value = value; self.rpt = rpt
def __call__ (self):
for i in range (self.rpt):
yield self.value
so I would do:
my_rpt_obj = obj (rpt ('hello', 5))
to repeat 'hello' 5 times (for example).
But this doesn't work. when obj calls self.gen(), that returns a generator, not
the next value.
How can I make this work? I can't change the interface of the existing class
obj, which expects a callable to get the next value.
--
-- Those who don't understand recursion are doomed to repeat it
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
basic generator question Neal Becker <ndbecker2@gmail.com> - 2015-02-04 08:23 -0500 Re: basic generator question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-05 03:09 +1100
csiph-web