[Python-Dev] Re: tarfile module (update)
Guido van Rossum
Wed, 27 Nov 2002 08:33:19 -0500
> using factory functions to create objects representing external
> entities is an extremely common pattern.
> in the Pythobn library, this pattern is used in aifc, anydbm, audiodev,
> dbhash, dumbdbm (and all other dbm modules), fileinput, gettext,
> gopherlib, gzip, imghdr, optparse, popen2, shelve, sndhdr, socket,
> sunau, sunaudio, tempfile, tokenize, just to name a few.
> to figure out *why* it's a good idea to use a factory function, think
> as a user. or as a library maintainer.
Actually, most of those cases (not all) are factory functions because
the type created is implemented in C, and until Python 2.2 the only
way to create an instance of such a type was a factory function. Note
that in 2.2, socket.socket changed from being a factory function to a
type with a default constructor, and nobody's code broke.
In fact, you can say that in Python, classes and types *are* factory
functions (no "operator new" is required).
--Guido van Rossum (home page: http://www.python.org/~guido/)