Previous code uses yield directly at the function level, but how to use yield after encapsulating class is rarely encountered.
After a half-day study, I finally finished the example. The code has no special place, just as a work item.
Working with the builder:
####################################################################### #class Detail (object): "" "" "" #----------- -----------------------------------------------------------def __init__ (self,qty): "" "" "Constructor" "" Sel F.qty = Qty ####################################################################### #class Bill (object): "" "" "" #----------------------------------------------------------------------def __init__ (self,no): "" "Constructo R "" "self.no = no Self.detail_lst = List () def adddetail (Self,qty): Self.detail_lst.append (Det AIL (qty)) ####################################################################### #class account (obj ECT): "" "" "" #----------------------------------------------------------------------def __init__ (self,total): "" "" "Constructor" "" Self.total = Total def dobuy (self,blllst): for OBJBLL in Blll St:self.total += 1 yield objbll ####################################################################### #class Pay (objec T): "" "" "#----------------------------------------------------------------------def __init__ (self):" "" Constructor "" "Pass Def Dopay (SELF,BLL): For OBJBLL in Bll:for (Inde X,detail) in Enumerate (OBJBLL.DETAIL_LST): yield Detail.qty acc = Account (0) pay = Pay () Blllst = List () BLL = Bill (1) BLL. Adddetail (5) BLL. Adddetail () blllst.append (BLL) BLL = Bill (2) BLL. Adddetail () BLL. Adddetail () blllst.append (BLL) Rmtpay = Pay.dopay (Acc.dobuy (blllst)) Paysum = 0for Qty in Rmtpay:paysum + = Qtyprint (' Co UNT:%d,sum:%d '% (Acc.total, paysum))
Cooperation with the association:
def coroutine (func): Def start (*args,**kwargs): G = func (*args,**kwargs) g.__next__ () return g return start####################################################################### #class Detail (object): "" "" "" "# ----------------------------------------------------------------------def __init__ (self,qty): "" "Constructor" "" Self.qty = Qty ####################################################################### #class Account (object): "" "" "#----------------------------------------------------------------------def __init__ (self,total):" "" Constructor "" "Self.total = Total @coroutine def Buy: while (True): Objdetail = (yield) if (Objdetail is None): Break self.total + = Objdetail.q Ty ####################################################################### #class Pay (object): "" "" "" #------- ---------------------------------------------------------------def __init__ (self,nextstop): "" "" "Constructor" "" Self.billcount = 0 Self.nextstop = Nextstop @coroutine def dopay (self): while (True): OBJD Etail = (yield) if (Objdetail is None): Break Self.billcount + = 1 Self.next Stop.send (objdetail) ACC = account (Ten) pay = Pay (acc. Buy ()) obj = Pay.dopay () Bill_1 = Detail (Ten) Obj.send (bill_1) bill_2 = Detail (+) obj.send (bill_2) print (' Count:%d, sum:%d ' % (Pay.billcount,acc.total))
Memo: Python 3 uses yield in class