Memo: Python 3 uses yield in class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.