[Tutor] More efficient than glob?

Doug.Shawhan@gecits.ge.com Doug.Shawhan@gecits.ge.com
Thu Nov 7 11:08:02 2002


Woah. Pretty neat.

What practical use for such a list is there?

d

-----Original Message-----
From: Y. Duppen [mailto:yduppen@refactive.com]
Sent: Thursday, November 07, 2002 9:58 AM
To: Shawhan, Doug (CAP, ITS, US); tutor@python.org
Subject: Re: [Tutor] More efficient than glob?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Heh. Good, because that would have lead me to the embarassing question:
> "What is a generator?" :-)

http://www.python.org/doc/current/whatsnew/node5.html

In effect, a generator allows you to create huge lists that take a minimal 
amount of memory.

for example:

from __future__ import generators
def infiniteList():
	start = 1
	while 1:
		yield start
		start += 1

This function is a generator (as can be seen by the use of the keyword 
'yield'). You usually use these functions in for-loops:

for i in infiniteList():
	print i

The alternative would of course be impossible: you can *not* generate an 
infinite list up front and then iterate over it. In the case of generators, 
elements are generated while you are iterating...

YDD
- -- 
http://www.refactive.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9yo1tLsKMuCf5EdwRAq1kAKCXJlcUxwLdihJazwgeBjCGuLV8TQCgu3HM
j8XVjhPPKa6v0yvtUmumNlU=
=bMYp
-----END PGP SIGNATURE-----