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


Groups > comp.lang.python > #39842

Re: yield expression

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <zlchen.ken@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'value,': 0.03; 'none:': 0.05; 'assign': 0.07; 'caller': 0.07; 'val': 0.07; 'commonly': 0.09; 'none.': 0.09; 'to:addr:comp.lang.python': 0.09; 'used).': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.10; 'value.': 0.15; '"none"': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'count)': 0.16; 'counter()': 0.16; 'execute,': 0.16; 'expression.': 0.16; 'folks,': 0.16; 'subject:expression': 0.16; 'subject:yield': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'yield': 0.17; '>>>': 0.18; 'otherwise,': 0.20; 'trying': 0.21; 'resumes': 0.22; 'sends': 0.22; 'monday,': 0.23; 'seems': 0.23; '(this': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '----': 0.27; 'cc:addr:gmail.com': 0.27; 'usually': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'received:google.com': 0.34; 'thanks': 0.34; 'feed': 0.35; 'next': 0.35; 'method': 0.36; 'should': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'skip:u 10': 0.60; 'first': 0.61; 'difficulty': 0.65; '2013': 0.84; 'oscar': 0.84; 'yielded': 0.84
X-Received by 10.49.15.38 with SMTP id u6mr664371qec.8.1361753946488; Sun, 24 Feb 2013 16:59:06 -0800 (PST)
Newsgroups comp.lang.python
Date Sun, 24 Feb 2013 16:59:06 -0800 (PST)
In-Reply-To <mailman.2469.1361753515.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=168.159.144.57; posting-account=EUV6tAoAAAA_IfJF_x-XYQxCI2bc4CDF
References <eb6313dd-94f9-406d-b353-3d64d171e0a7@googlegroups.com> <mailman.2469.1361753515.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 168.159.144.57
MIME-Version 1.0
Subject Re: yield expression
From Ziliang Chen <zlchen.ken@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Ziliang Chen <zlchen.ken@gmail.com>, Python List <python-list@python.org>
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.2470.1361753950.2939.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361753950 news.xs4all.nl 6879 [2001:888:2000:d::a6]:45821
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39842

Show key headers only | View raw


On Monday, February 25, 2013 8:51:28 AM UTC+8, Oscar Benjamin wrote:
> On 25 February 2013 00:39, Ziliang Chen  wrote:
> 
> > Hi folks,
> 
> > When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be "None" all the time.
> 
> >
> 
> > Thanks !
> 
> >
> 
> > code snippet:
> 
> > ----
> 
> >  def counter(start_at=0):
> 
> >      count = start_at
> 
> >      while True:
> 
> >          val = (yield count)
> 
> >          if val is not None:
> 
> >              count = val
> 
> >          else:
> 
> >              print 'val is None'
> 
> >              count += 1
> 
> 
> 
> The value of the yield expression is usually None. yield only returns
> 
> a value if the caller of a generator function sends one with the send
> 
> method (this is not commonly used). The send method supplies a value
> 
> to return from the yield expression and then returns the value yielded
> 
> by the next yield expression. For example:
> 
> 
> 
> >>> g = counter()
> 
> >>> next(g)  # Need to call next() once to suspend at the first yield call
> 
> 0
> 
> >>> g.send('value for count')  # Now we can send a value for yield to return
> 
> 'value for count'
> 
> 
> 
> 
> 
> Oscar

Thanks Oscar !
I am cleared. Only when "send" is used to feed "yield" a new value, the "yield" expression has none "None", otherwise, "yield" expression has "None" value.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:39 -0800
  Re: yield expression Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-25 00:51 +0000
    Re: yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:59 -0800
    Re: yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:59 -0800

csiph-web