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


Groups > comp.lang.python > #98847

Question about yield

X-Received by 10.66.160.3 with SMTP id xg3mr25273750pab.35.1447555054520; Sat, 14 Nov 2015 18:37:34 -0800 (PST)
X-Received by 10.50.126.99 with SMTP id mx3mr199980igb.1.1447555054489; Sat, 14 Nov 2015 18:37:34 -0800 (PST)
Path csiph.com!au2pb.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!i2no3321417igv.0!news-out.google.com!f6ni2809igq.0!nntp.google.com!i2no3321412igv.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Sat, 14 Nov 2015 18:37:33 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=50.100.117.144; posting-account=SZ_svQkAAACWRFG2bDA-zgq8ILyl4-vo
NNTP-Posting-Host 50.100.117.144
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <ac359441-8cca-496c-b5c2-d3e7d7ce15dd@googlegroups.com> (permalink)
Subject Question about yield
From fl <rxjwg98@gmail.com>
Injection-Date Sun, 15 Nov 2015 02:37:34 +0000
Content-Type text/plain; charset=ISO-8859-1
X-Received-Bytes 2123
X-Received-Body-CRC 1193768549
Xref csiph.com comp.lang.python:98847

Show key headers only | View raw


Hi,
I have read a couple of tutorial on yield. The following code snippet still
gives me a shock. I am told yield is a little like return. I only see one 
yield in the tutorial examples. Here it has two yields. And there are three
variables following keyword yield.
I have not tried debug function to get the running states yet.
Could you give me some explanation on this yield usage?
Or are there some high relevant tutorial on this?

Thanks, 



def pfr(sequence, pos, stepsize, n):
  seq = iter(sequence)
  x = ones((n, 2), int) * pos                   # position
  f0 = seq.next()[tuple(pos)] * ones(n)         # model
  yield pos, x, ones(n)/n                       # weights
  for im in seq:
    x += uniform(-stepsize, stepsize, x.shape)  # 
    x  = x.clip(zeros(2), array(im.shape)-1).astype(int) # 
    f  = im[tuple(x.T)]                         # s
    w  = 1./(1. + (f0-f)**2)                    # distance
    w /= sum(w)                                 # w
    yield sum(x.T*w, axis=1), x, w              # weights
    if 1./sum(w**2) < n/2.:                     # degenerate:
      x  = x[res(w),:]                     # to weights

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


Thread

Question about yield fl <rxjwg98@gmail.com> - 2015-11-14 18:37 -0800
  Re: Question about yield Chris Angelico <rosuav@gmail.com> - 2015-11-15 13:43 +1100
  Re: Question about yield Steven D'Aprano <steve@pearwood.info> - 2015-11-17 00:10 +1100

csiph-web