Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; '(self,': 0.09; '__init__': 0.09; 'obj': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; '(self):': 0.16; 'a()': 0.16; 'callable': 0.16; 'example).': 0.16; 'expects': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hits': 0.16; 'obj,': 0.16; 'subject:generator': 0.16; 'thinking,': 0.16; 'value;': 0.16; 'zero.': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'value.': 0.19; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; '(for': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; 'class': 0.32; 'figure': 0.32; 'interface': 0.32; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'next': 0.36; 'so,': 0.37; 'work?': 0.38; 'track': 0.38; 'how': 0.40; 'range': 0.61; 'times': 0.62; 'repeat': 0.74; '2015': 0.84; 'self.value': 0.84; 'do:': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=C2bCXOOyQCr9SvpZpOrkyUW2upUEll+NKhwNRvBlwgI=; b=Dmk5oKAts7+uZXTC1acuBcaXyQrfCjy+brjyHgS1seveOvx4SzXStUPwl3rKjBTB71 0Ls+WUx8EMpFMGCa+6z8d4LAjWBpdhDOQNc2Dy+7YeX4nBineCwyzcATi7pObcf/mJfv 3L5Q/m5LlAtXgk49mqozch6YSphh4rVrmup43eRln2ZLgm68Yphj6ePaq4Z0Wh8gqyZu rpnpmFc4NkzQ01u4pVcahH39nq6K7jVClRTgyyfTHPMbH9SLjtp+EWXs33zCrtDuR7pS zOirk0rqzQXWsHkOj195uylNjy4KBy/KKlkzoHdrB4ypz0UCIpKiSnp/aP/9LbsBY19r lw5A== MIME-Version: 1.0 X-Received: by 10.107.167.201 with SMTP id q192mr35776552ioe.43.1423060348495; Wed, 04 Feb 2015 06:32:28 -0800 (PST) In-Reply-To: References: Date: Thu, 5 Feb 2015 01:32:28 +1100 Subject: Re: basic generator question From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423060351 news.xs4all.nl 2926 [2001:888:2000:d::a6]:41063 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85215 On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker wrote: > 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. So, if I understand you correctly, you want your rpt object to return 'hello' five times to five consecutive calls? >>> a = rpt() >>> a() 'hello' >>> a() 'hello' >>> a() 'hello' >>> a() 'hello' >>> a() 'hello' You could do that with a generator by repeatedly calling next() on it, or you could just keep track of the number of times you were called: class rpt: def __init__ (self, value, rpt): self.value = value; self.rpt = rpt def __call__ (self): if self.rpt: self.rpt -= 1 return self.value # ... otherwise? Up to you to figure out what to do when self.rpt hits zero. ChrisA