Python Forum
Create Generator in the Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create Generator in the Class
#1
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?
Reply
#2
Change the return to yield and have it yield a value.

Here is a brief tutorial on using generators and yield
quest likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,492 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Tuple generator, and function/class syntax quazirfan 3 3,854 Aug-10-2021, 09:32 AM
Last Post: buran
  Trying to access next element using Generator(yield) in a Class omm 2 1,965 Oct-19-2020, 03:36 PM
Last Post: omm
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,029 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  How to create a varying qty of instances of a Class and name them? KM007 2 2,033 May-29-2020, 12:30 PM
Last Post: KM007
  How I can create reference to member of the class instance? AlekseyPython 8 3,810 Dec-05-2018, 06:24 AM
Last Post: AlekseyPython
  receive from a generator, send to a generator Skaperen 9 5,494 Feb-05-2018, 06:26 AM
Last Post: Skaperen
  Trying to create a child class in Python 2.7 mac77 1 2,886 Sep-27-2017, 06:15 PM
Last Post: nilamo
  Create class instances from list of strings pythonck 1 3,596 Sep-18-2017, 06:13 PM
Last Post: ichabod801
  How to Create Very Very Special Class with too many magic methods ? harun2525 5 4,346 Apr-13-2017, 10:18 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020