From: Ian Kelly on
2010/7/4 CHEN Guang <dr.cg(a)126.com>:
> Why Python forbids multiple instances of one module?
> If only Python allows multiple instances of one module, module will
> be enough to replace class in most cases.
> After all, it is much easier to write a module than a class, at least we do
> not have to write self everywhere.

If you really want to do that, it should be possible by deleting the
entry from sys.modules and re-importing it. You save yourself having
to explicitly write self everywhere, but instead you have to declare
all your "instance" variables as globals in each "method" that uses
them, which isn't much less of a chore. You also lose inheritance,
properties (and descriptors in general), magic method support,
metaclasses, and pretty much all the other nice features that
new-style classes have to offer.
From: Chris Rebert on
2010/7/4 CHEN Guang <dr.cg(a)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
From: kedra marbun on
module obj is instance of types.ModuleType, which is instance of
'type', where class obj is instance of 'type'. even only at this
point, they're diff in to many ways. there are so many things to do
when you truly want module to replace class, as pointed by 2 posts
above

i'm also a beginner, so i can offer you a tip in learning it: follow
the design, py usually has good reasons about it, i've lost in every
challenge i put against py design, luckily i find the reasons through
my own experiment. yes, it's better to find it through hacking, rather
than people talking about it, the reason is the same as coding to
learn programming, not only reading book. it will become more
understandable when people say py has clean design

as for 'self', yes i admit it seems troublesome at first, you'll have
reason for it once you know that method is simply a func, you'll
embrace the more general design (py requires you to specify a name for
the caller) when you learn about metaclass. a nice argument to this is
that human beings are visual creatures, it becomes so much easier to
have a name that desribes the obj it points to (like 'self', 'cls',
'metacls') than implicit name that trying to be as general as possible
(like 'this')