<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 4, 2015 at 10:19 AM, Joel Goldstick <span dir="ltr"><<a href="mailto:joel.goldstick@gmail.com" target="_blank">joel.goldstick@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Wed, Feb 4, 2015 at 9:32 AM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker <<a href="mailto:ndbecker2@gmail.com" target="_blank">ndbecker2@gmail.com</a>> wrote:<br>
> Now I want gen to be a callable that repeats N times. I'm thinking, this<br>
> sounds perfect for yield<br>
><br>
> class rpt:<br>
> def __init__ (self, value, rpt):<br>
> self.value = value; self.rpt = rpt<br>
> def __call__ (self):<br>
> for i in range (self.rpt):<br>
> yield self.value<br>
><br>
> so I would do:<br>
><br>
> my_rpt_obj = obj (rpt ('hello', 5))<br>
><br>
> to repeat 'hello' 5 times (for example).<br>
><br>
> But this doesn't work. when obj calls self.gen(), that returns a generator, not<br>
> the next value.<br>
><br>
> How can I make this work? I can't change the interface of the existing class<br>
> obj, which expects a callable to get the next value.<br></span></blockquote><div><br></div></span><div>Can you actually show your code and the traceback? </div></div></div></div></blockquote><div><br></div><div>When you do rpt('hello',5)) you create an instance of rpt but you don't actually invoke it.</div><div><br></div><div>maybe try:</div><div><br></div><div>my_rpt_obj = obj(rpt('hello',5))()</div><div><br></div><div>If I'm thinking straight, its the final () that actually cause your __call__ to happen. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>
<br>
</span>So, if I understand you correctly, you want your rpt object to return<br>
'hello' five times to five consecutive calls?<br>
<br>
>>> a = rpt()<br>
>>> a()<br>
'hello'<br>
>>> a()<br>
'hello'<br>
>>> a()<br>
'hello'<br>
>>> a()<br>
'hello'<br>
>>> a()<br>
'hello'<br>
<br>
You could do that with a generator by repeatedly calling next() on it,<br>
or you could just keep track of the number of times you were called:<br>
<span><br>
class rpt:<br>
def __init__ (self, value, rpt):<br>
self.value = value; self.rpt = rpt<br>
def __call__ (self):<br>
</span> if self.rpt:<br>
self.rpt -= 1<br>
return self.value<br>
# ... otherwise?<br>
<br>
Up to you to figure out what to do when self.rpt hits zero.<br>
<br>
ChrisA<br>
<span><font color="#888888">--<br>
<a href="https://mail.python.org/mailman/listinfo/python-list" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div></div></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div>Joel Goldstick<br></div><a href="http://joelgoldstick.com" target="_blank">http://joelgoldstick.com</a><br></div></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Joel Goldstick<br></div><a href="http://joelgoldstick.com" target="_blank">http://joelgoldstick.com</a><br></div></div>
</div></div>