Bob Gailer wrote: > Asrarahmed Kadri wrote: >> >> What are generators and iterators...??And why are they are needed..?? >> >> I can do my stuff with a 'for' or a 'while' loop.. so why do I need an >> ITERATOR..? > iterators & generators do not replace while or for loops!
No, actually iterators and generators are the foundation of for loops. Whenever you write a for loop, Python creates an iterator to return the values that are assigned to the loop variables. Writing your own iterator is a way to make a new kind of thing that can be iterated in a for loop. Generators are a convenient way to make iterators. They can be used to encapsulate loop logic. For some examples see these recent threads: http://thread.gmane.org/gmane.comp.python.tutor/36068/focus=36069 http://article.gmane.org/gmane.comp.python.tutor/36105/match=generator >> >> And what is a generator ..? I did try to read about these two things >> on the web, but still I AM CONFUSED. > Well at risk of offering more web stuff, try > http://heather.cs.ucdavis.edu/~matloff/Python/PyIterGen.pdf Thanks, that is very nice! Kent >> To HIM you shall return. > I am glad that this thought brings you some comfort or stimulation. I > used to believe something similar, and all it did was bring me fear > guilt and shame. > > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor