Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85210

basic generator question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; '(self,': 0.09; '__init__': 0.09; 'obj': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'def': 0.12; '(self):': 0.16; 'callable': 0.16; 'example).': 0.16; 'expects': 0.16; 'obj,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:generator': 0.16; 'thinking,': 0.16; 'value;': 0.16; 'value.': 0.19; 'header:User- Agent:1': 0.23; '(for': 0.26; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; "i'm": 0.30; 'work.': 0.31; 'class': 0.32; 'interface': 0.32; "can't": 0.35; 'but': 0.35; 'yield': 0.36; 'next': 0.36; 'work?': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'range': 0.61; 'times': 0.62; 'repeat': 0.74; 'received:139': 0.84; 'self.value': 0.84; 'do:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Neal Becker <ndbecker2@gmail.com>
Subject basic generator question
Date Wed, 04 Feb 2015 08:23:40 -0500
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host exa2-in-fw-01-epn.hns.com
User-Agent KNode/4.14.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18458.1423056240.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423056240 news.xs4all.nl 2970 [2001:888:2000:d::a6]:42717
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85210

Show key headers only | View raw


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 | NextNext in thread | Find similar | Unroll thread


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