Python Forum

Full Version: Create Generator in the Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a class and inside this class I have this function:

def ff(self, timelimit):
    
     while self.lasttime < timelimit :
         ... do your stuff                   
     self.otherpart.messagelist.append((self.lasttime + self.delaytime, self.fakeid))
     return None 
         
And of course I have also init and iter function inside my class
Apart from that, I have also an external function which is using my ff fucntion:

def Simulatetilltime(A, timelimit):
     B=A.otherport
     Alist = []
     Blist = []
     A.lastt
     
     while A.lasttime < timelimit :
       try:
          if A.lasttime <= B.lasttime :
              x=A.ff( B.lasttime+A.delaytime)
              if x!=None: Alist.append(x)
          else : 
              print("Here we are****************************************************")
              x=B.ff( A.lasttime+B.delaytime)
              if x!=None: Blist.append(x)
       except StopIteration: break       
     return Alist, Blist
I want to create a generator inside the class instead of using just function. How can I do that?
Change the return to yield and have it yield a value.

Here is a brief tutorial on using generators and yield