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


Groups > comp.lang.python > #98847

Question about yield

Newsgroups comp.lang.python
Date 2015-11-14 18:37 -0800
Message-ID <ac359441-8cca-496c-b5c2-d3e7d7ce15dd@googlegroups.com> (permalink)
Subject Question about yield
From fl <rxjwg98@gmail.com>

Show all headers | 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