Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
Kent Johnson wrote: > 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 Here is another good article about what happens behind the scenes of a for loop: http://online.effbot.org/2006_11_01_archive.htm#for Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
> My understanding is that an iterator is basically a facade` pattern. > If you aren't familiar with patterns, a facade` pattern basically makes > something really easy to use or convenient. Yes, you can "do it by > hand", and many times that is indeed the preferred method, but sometimes > it's easier to use an iterator...particularly if it isn't a special > case. Nothing in the manual says you HAVE to use iterators, but they > *can* make life easier. Concretely, we can write a program that will find all the even-length strings in some sequence, and give it back to us as a list: def filterEvenStrings(sequence): results = [] for x in sequence: if len(x) % 2 == 0: results.append(x) return results Now, we can use this function on a list of strings, as one would expect: ### >>> filterEvenStrings('hello world this is a test'.split()) ['this', 'is', 'test'] ### But we can also use this on a file: ## >>> evenWords = filterEvenStrings( ... [x.strip() ... for x in open('/usr/share/dict/words').readlines()]) >>> evenWords[:10] ['aa', 'Aani', 'aardvark', 'aardwolf', 'Aaronite', 'Aaru', 'Ab', 'Ababua', 'abac', 'abacay'] ## Note the nice thing here: we've been able to reuse filterEvenStrings on two entirely different things! We can use the same function on different things because those two things support a common interface: "iteration" support. Most of Python's interesting data structures have built-in iterators. But there are plenty of things that don't by default, but for which we might want to add such iteration support. 'for' loops expect things that iterate: if we try to apply them on things that don't, we'll get an error. For example: ## >>> class Candy: ... def __init__(self, name): ... self.name = name ... >>> class CandyBag: ... def __init__(self): ... self.contents = [] ... def addCandy(self, aCandy): ... if isinstance(aCandy, Candy): ... self.contents.append(aCandy) ... ### Ok, so we can make a CandyBag, and we might like to start adding things to it: ### >>> bag = CandyBag() >>> bag.addCandy(42) >>> bag.addCandy(Candy("snickers")) >>> bag.addCandy(Candy("salt water taffy")) >>> bag.addCandy("spam") ### We'd expect 42 and spam to be ignored, because they're not Candy. Anyway, so let's go over the bag with a loop: ### >>> for c in bag: ... print c ... Traceback (most recent call last): File "", line 1, in ? TypeError: iteration over non-sequence ### This bag is not a sequence. It may CONTAIN a sequence, but it itself isn't one. Now, we can either pull out the bag.contents if we're naughty, but we want to be good: let's have CandyBag support iteration. class CandyBag: def __init__(self): """Creates an empty CandyBag.""" self.contents = [] def addCandy(self, aCandy): """Adds aCandy if it's a Candy. Otherwise, ignore it.""" if isinstance(aCandy, Candy): self.contents.append(aCandy) def __iter__(self): """Returns an iterator to all the candy in us.""" return iter(self.contents) We've added a method __iter__() that produces an iterator when requested. In this case, we'll reuse the built-in iterator for lists. The iter() function takes anything that supports iteration, and gives us a iterator: ## >>> myiterator = iter([3, 1, 4]) >>> myiterator >>> myiterator.next() 3 >>> myiterator.next() 1 >>> myiterator.next() 4 >>> myiterator.next() Traceback (most recent call last): File "", line 1, in ? StopIteration ## And now that CandyBags support iteration, we can satisfy our sweet tooth and loop over a CandyBag using our for loop: ### >>> bag = CandyBag() >>> bag.addCandy(42) >>> bag.addCandy(Candy("snickers")) >>> bag.addCandy(Candy("salt water taffy")) >>> bag.addCandy("spam") >>> for c in bag: ... print c ... <__main__.Candy instance at 0x22893f0> <__main__.Candy instance at 0x22896c0> ### There's our two pieces of candy. We didn't add str() support to our Candy, so that's why they're printing in that peculiar form. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
My understanding is that an iterator is basically a facade` pattern. If you aren't familiar with patterns, a facade` pattern basically makes something really easy to use or convenient. Yes, you can "do it by hand", and many times that is indeed the preferred method, but sometimes it's easier to use an iterator...particularly if it isn't a special case. Nothing in the manual says you HAVE to use iterators, but they *can* make life easier. Jonathon Asrarahmed Kadri wrote: > > > Hi Folks, > > 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..? > > And what is a generator ..? I did try to read about these two things on > the web, but still I AM CONFUSED. > > To be honest, I am used to the nice and lucid style of the wonderful > people on this forum.. :)- > > Regards, > Asrarahmed Kadri > > > -- > To HIM you shall return. > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
> 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..? > > And what is a generator ..? I did try to read about these two things on the > web, but still I AM CONFUSED. here are some more places to look in addition to what others have already posted... 1. i wrote a short article in Linux Journal back when Python 2.2 was released, highlighting its new features; it includes a discussion and examples of iterators and generators: http://www2.linuxjournal.com/article/5597 my short take on it is that iterators let you use for-loops over sequence-*like* objects. in older versions of Python, they had to *be* sequences, i.e. strings, lists, tuples. but with iterators, you can now go through lines of a file, keys of a dictionary, etc. and generators are like "iterators with brains" because you can "generate" successive items to be iterated over, in addition to the cool co-routine-like resumable function features. 2. i do Python training professionally, and for people considering my services, i offer a free 5 minute sample podcast/video of what it's like to take a course from me. the example filmed is where i discuss generators right after i introduce iterators. http://cyberwebconsulting.com (click "Python Training") hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
May I invite EVERYONE to NOT introduce political or religious elements into these discussions. It has no place here and can destroy this excellent discussion group. I've seen it happen. PLEASE take it elsewhere. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bob Gailer Sent: Friday, October 27, 2006 10:11 AM To: Asrarahmed Kadri Cc: pythontutor Subject: Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me Asrarahmed Kadri wrote: > > > Hi Folks, > > 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! > > 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 > > 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. -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
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
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
Asrarahmed Kadri wrote: > > > Hi Folks, > > 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! > > 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 > > 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. -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] I am terribly confused about "generators" and "iterators".. Help me
Hi Folks, 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..? And what is a generator ..? I did try to read about these two things on the web, but still I AM CONFUSED. To be honest, I am used to the nice and lucid style of the wonderful people on this forum.. :)- Regards, Asrarahmed Kadri -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor