Why Python forbids multiple instances of one module?

Chris Rebert clp2 at rebertia.com
Mon Jul 5 01:49:54 EDT 2010


2010/7/4 CHEN Guang <dr.cg at 126.com>:
> Why Python forbids multiple instances of one module?

That's just how its import mechanism works. It allows for modules that
need canonical program-wide state to rely on being singleton, and it's
also an optimization.
You can trick the import machinery and get around the restriction though.

> If only Python allows multiple instances of one module, module will
> be enough to replace class in most cases.

Why do classes need replacement in the first place? Modules and
classes serve distinct purposes.

Also: How would you specify a class-module's superclasses?
And what if a class depends on other modules/classes/packages? Then
the class-module's namespace would be polluted with all the stuff it
imported; ick.

> After all, it is much easier to write a module than a class, at least we do
> not have to write self everywhere.

That seems a relatively petty reason; see also Ian's excellent point
about `global`.

Cheers,
Chris
--
Yay, Fireworks!
http://blog.rebertia.com



More information about the Python-list mailing list