Am Mittwoch, 3. März 2010 21:41:34 schrieb Dave Angel: > John wrote: > > Hi, > > > > I just read a few pages of tutorial on list comprehenion and generator > > expression. From what I gather the difference is "[ ]" and "( )" at the > > ends, better memory usage and the something the tutorial labeled as "lazy > > evaluation". So a generator 'yields'. But what is it yielding too? > > > > John > > A list comprehension builds a whole list at one time. So if the list > needed is large enough in size, it'll never finish, and besides, you'll > run out of memory and crash. A generator expression builds a function > instead which *acts* like a list, but actually doesn't build the values
Well, it act like an iterable. A list-like object would probably have __getitem__ and friends. Historically (as in I doubt you'll find a version that still does it that way in the wild, I looked it up, guess around 2.2 iterators were introduced), the for loop in python has been doing a "serial" __getitem__ call. Nowadays, for i in x: print i does something like this: x_it = iter(x) # get an iterator for x try: while True: i = x_it.next() print i except StopIteration: pass As you can see, x_it (and the underlying x) do not need to provide all items in memory at the same time. Andreas _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor