Python Forum
making a generator class?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a generator class?
#1
we can make a generator function by making a function with a yield statement. what about making a class that way? can i make a class that is a generator? there is a method that i want to add to my class that modifies what is being generated. is this possible in a class method?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If I understand you correctly, it isn't a problem. You mean like to make a Calculator class and feed numbers to it's methods? That should work just fine.
Reply
#3
not that. i mean to make a generator that generates a musical tune and has a method to change the key "on the fly" (that is a made-up example that is more accurate).
foo = generate_tune("happy birthday")
my_key = 'A'
foo.set_key(mykey)
now = time()
for sound_sample in foo:
    play_sample(sound_sample,sound_sample)
    if time()-now > 10 and mykey == 'A':
        mykey = 'A#'
        foo.set_key(mykey)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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