Question about yield

fl rxjwg98 at gmail.com
Sat Nov 14 21:37:33 EST 2015


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



More information about the Python-list mailing list