备忘录:python 3在class中使用yield

本文介绍了如何在Python中利用生成器和协程实现复杂的业务流程处理。通过具体实例展示了生成器在类方法中的应用,并进一步讲解了协程在状态流转中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前代码都是直接在函数级别使用yield,但封装class后如何使用yield很少遇到。

经过半天的学习,总算完成示例。代码没有什么特殊地方,仅仅作为一个工作项。

与生成器合作:

 

########################################################################
class Detail(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self,qty):
        """Constructor"""
        self.qty = qty 
        
########################################################################
class  Bill(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self,no):
        """Constructor"""
        self.no = no
        self.detail_lst = list()
    
    def AddDetail(self,qty):
        self.detail_lst.append(Detail(qty))   
        
  
       
    
########################################################################
class Account(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self,total):
        """Constructor"""
        self.total = total       
    
 
    def doBuy(self,BllLst):         
        for objBll in BllLst:
            self.total += 1
            yield objBll
        
########################################################################
class Pay(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        pass
        
    
    def doPay(self,bll):             
        for objBll in bll:
            for (index,detail) in enumerate(objBll.detail_lst):
                yield detail.qty   
                
                
        
acc = Account(0)
pay = Pay()

bllLst = list()

bll = Bill(1)
bll.AddDetail(5)
bll.AddDetail(10)
bllLst.append(bll)

bll = Bill(2)
bll.AddDetail(15)
bll.AddDetail(20)

bllLst.append(bll)


rmtPay = pay.doPay(acc.doBuy(bllLst))

paySum = 0
for qty in rmtPay:
    paySum += qty

print('count: %d,sum : %d' % (acc.total, paySum))      
    

  

与协程的合作:

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(self):        
        while(True):        
            objDetail = (yield)
            if(objDetail is None):
                break
            self.total += objDetail.qty
        
########################################################################
class Pay(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self,nextStop):
        """Constructor"""
        self.billCount = 0
        self.nextStop = nextStop
        
    @coroutine
    def doPay(self):      
        while(True):        
            objDetail = (yield)
            if(objDetail is None):
                break
            self.billCount += 1
            self.nextStop.send(objDetail)
        
    
acc = Account(10)
pay = Pay(acc.Buy())


obj = pay.doPay()
bill_1 = Detail(10)
obj.send(bill_1)
bill_2 = Detail(20)
obj.send(bill_2)

print('count : %d , sum : %d' % (pay.billCount,acc.total))
        

  

转载于:https://www.cnblogs.com/febwave/p/5051331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值