Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is a class?

6 views
Skip to first unread message

casti...@gmail.com

unread,
Mar 5, 2008, 1:50:12 PM3/5/08
to
What is a class that is not a module?

Paul McGuire

unread,
Mar 5, 2008, 2:29:30 PM3/5/08
to
On Mar 5, 12:50 pm, castiro...@gmail.com wrote:
> What is a class that is not a module?

Please stop posting these one-liner beginner questions. If you can
type it in one line, you can enter it on the Google.com or Ask.com
query page and get a wealth of *existing* information, from tutorials,
documentation, online presentations. If you are able to phrase such a
question, you are capable of doing a little research and
experimentation on your own.

These posts translate to "I'm too lazy to use Google, or too cheap to
buy a Python book, or too lazy to read it, or too impatient to do my
own experimenting - much better to just post on c.l.py and have the
answer spoon-fed to me!"

I refuse to spoon feed you the answer to this question when plenty of
supporting material is already available.

-- Paul

casti...@gmail.com

unread,
Mar 5, 2008, 3:04:30 PM3/5/08
to

In the future, shall I assume that other readers here have no ideas
(on this, read *ever*), that haven't already been published? 'Cause I
have.

For instance, in this example, I've tried a few approaches that didn't
turn out well.

Do you want a comparison of existing solutions? Do you have a proof
that they exhaust the solution space?

I'm willing to address convention, in serial or parallel--- (change
subject to 'what goes on newsgroups'?), but it's not clear from fact
what assumption who has made.

Next time, why don't you say, "How much experience do you have?", or
"What level should I gear my answer toward?"

Paul McGuire

unread,
Mar 5, 2008, 4:12:43 PM3/5/08
to
On Mar 5, 2:04 pm, castiro...@gmail.com wrote:
> On Mar 5, 1:29 pm, Paul McGuire <pt...@austin.rr.com> wrote:
>
> > On Mar 5, 12:50 pm, castiro...@gmail.com wrote:
>
> > > What is a class that is not a module?
>
> > Please stop posting these one-liner beginner questions.  If you can
> > type it in one line, you can enter it on the Google.com or Ask.com
> > query page and get a wealth of *existing* information, from tutorials,
> > documentation, online presentations.  If you are able to phrase such a
> > question, you are capable of doing a little research and
> > experimentation on your own.
>
> > These posts translate to "I'm too lazy to use Google, or too cheap to
> > buy a Python book, or too lazy to read it, or too impatient to do my
> > own experimenting - much better to just post on c.l.py and have the
> > answer spoon-fed to me!"
>
> > I refuse to spoon feed you the answer to this question when plenty of
> > supporting material is already available.
>
> In the future, shall I assume that other readers here have no ideas
> (on this, read *ever*), that haven't already been published?  'Cause I
> have.
>
> For instance, in this example, I've tried a few approaches that didn't
> turn out well.
>
Fair game. Capture one of these classes (or better, distill it down
to a *small* example demonstrating the problem), tell us what you are
trying to achieve, post it, AND tell us what is "not turning out
well." (Your previous posts have done a poor job in providing one or
more of these elements.)

> Do you want a comparison of existing solutions?  Do you have a proof
> that they exhaust the solution space?
>

*I* don't want *any* such thing, especially on the topics of "what is
a class?" and "how is a class different from a module?", nor do I need
proof that the basic background info on this topic covers the
mainstream applications of classes and modules. These are basic OO
concepts in Python. Try googling "python object-oriented". And if
there were some esoteric aspect of "what is a class?" that you want to
pursue that might not be covered in the available material, I would
expect one to include that in the original post.

> I'm willing to address convention, in serial or parallel--- (change
> subject to 'what goes on newsgroups'?), but it's not clear from fact
> what assumption who has made.
>

Since you did not elaborate on what your efforts were and the extent
they were undesirable (certainly useful info from someone honestly
interested in a helpful answer), I assumed you had made none.

> Next time, why don't you say, "How much experience do you have?", or
> "What level should I gear my answer toward?"
>

I put the onus on the poster to realize that the question they are
asking is in fact a basic concept, *especially* when the subject of
the question is captured in a built-in method, type, class, or
keyword. Surely you are aware that Python has been around for a
number of years, and that in all that time, it is entirely likely that
the topic of "what is a class?" has been covered, in any number of the
tutorials and docs so charitably written by many of the contributors
in this newsgroup.

Please stop asking those people to further expend their time repeating
this work just for your benefit. It is like arriving late to a
meeting, and asking everyone to stop and bring you up to speed on what
you missed, in effect saying, "My time is more valuable than yours, I
can't be bothered to arrive on time, or do even the simplest research
for myself."

-- Paul

Mike Driscoll

unread,
Mar 5, 2008, 4:38:59 PM3/5/08
to
On Mar 5, 12:50 pm, castiro...@gmail.com wrote:
> What is a class that is not a module?

Why is a raven like a writing desk?

As for Python class information, I would recommend reading the
following sites:

http://docs.python.org/tut/node11.html
http://www.diveintopython.org/object_oriented_framework/defining_classes.html

And for modules, check these out:

http://docs.python.org/tut/node8.html
http://www.penzilla.net/tutorials/python/modules/

Mike

casti...@gmail.com

unread,
Mar 5, 2008, 4:49:20 PM3/5/08
to
> > > > What is a class that is not a module?
>
> > I'm willing to address convention, in serial or parallel--- (change
> > subject to 'what goes on newsgroups'?), but it's not clear from fact
> > what assumption who has made.
>
> Since you did not elaborate on what your efforts were and the extent
> they were undesirable (certainly useful info from someone honestly
> interested in a helpful answer), I assumed you had made none.

I agree. To one of my friends, there's plenty of information in the
context that isn't a newsgroup. So I'll state more about my
background.

Classes and modules are really similar. In Python they're really
*really* similar.

Actually, at this point, that observation may have more of a
subjective component than I'm used to asserting. I pause here for
corroboration and others' perspectives. Aren't they?

D'Arcy J.M. Cain

unread,
Mar 5, 2008, 4:51:17 PM3/5/08
to Mike Driscoll, pytho...@python.org
On Wed, 5 Mar 2008 13:38:59 -0800 (PST)
Mike Driscoll <kyos...@gmail.com> wrote:
> On Mar 5, 12:50 pm, castiro...@gmail.com wrote:

I'm not sure why you change the address like this but could you please
include the full address. Those of us who identify the time wasters
would also like to drop the responses to their posts and changing the
address makes this impossible.

--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

Steven D'Aprano

unread,
Mar 5, 2008, 4:58:29 PM3/5/08
to
On Wed, 05 Mar 2008 10:50:12 -0800, castironpi wrote:

> What is a class that is not a module?

Er, all of them?

I'm curious what classes you think are modules.


--
Steven

Mike Driscoll

unread,
Mar 5, 2008, 5:00:17 PM3/5/08
to
On Mar 5, 3:51 pm, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Wed, 5 Mar 2008 13:38:59 -0800 (PST)
>
> Mike Driscoll <kyoso...@gmail.com> wrote:
> > On Mar 5, 12:50 pm, castiro...@gmail.com wrote:
>
> I'm not sure why you change the address like this but could you please
> include the full address. Those of us who identify the time wasters
> would also like to drop the responses to their posts and changing the
> address makes this impossible.
>
> --
> D'Arcy J.M. Cain <da...@druid.net> | Democracy is three


What are you talking about? I didn't change the address at all. I'm
not even sure what you mean. Are you talking about the post subject
line (which I have never touched in any post)? If you're talking about
the OP's email address, that's Google's fault for cropping them.

Mike

Paul McGuire

unread,
Mar 5, 2008, 5:14:23 PM3/5/08
to
On Mar 5, 3:49 pm, castiro...@gmail.com wrote:
>
> Classes and modules are really similar.  In Python they're really
> *really* similar.
>
> Actually, at this point, that observation may have more of a
> subjective component than I'm used to asserting.  I pause here for
> corroboration and others' perspectives.  Aren't they?

If all you use classes for is for the purpose of a namespace, and all
of the methods in the class are just staticmethods, then you have used
a class to replicate a module, and they aren't just really, *really*
similar, they are the pretty much the same (at least BEHAVIORALLY
speaking).

But you can't do this:

import Zmodule
zobject = Zmodule()

If you are really using the OO model, and creating instances of
classes, then you *have* to use a class, a module wont cut it. I'd
say, the less you use a class as an instance factory, the more similar
that class is to a module.

And you can't do this:

class Zclass:

import Zclass

If Zclass is not defined in your local script, you *have* to know what
module it is in, as in:

from zpackage.zmodule import Zclass

I have seen modules compared to classes that implement a Singleton
pattern. In this case, a class is being used in an intentionally
degenerate way, such that it creates only one instance. When doing
this in Python, one has the choice of replicating the standard pattern
with a class, or more simply, just using a module. (Python's language/
object model also permits another option, usually referred to as the
Borg implementation, in which many Python names bind to many Python
instances, but all share the same underlying object state. But this
is tangential to your question.)

In general I would say the similarity is a behavioral one, an artifact
of the language implementation. Modules are for code packaging and
namespace definition, classes are for type definition and object
modeling.

For more discussions about how similar classes are to modules, you
might google for "Python singleton".

-- Paul

casti...@gmail.com

unread,
Mar 5, 2008, 5:16:36 PM3/5/08
to
On Mar 5, 3:58 pm, Steven D'Aprano <st...@REMOVE-THIS-

Thank you for your time in entertaining my questions so far.

Functions are first-class objects.
Modules are first-class objects.
Classes are first-class objects.

There's even a ModuleType in 'types'.

>>> set( dir(type('None',(),{})) )- set( dir( ModuleType ) )
{'__module__', '__weakref__'}

How superficial are the differences?

Steven D'Aprano

unread,
Mar 5, 2008, 5:25:53 PM3/5/08
to
On Wed, 05 Mar 2008 13:49:20 -0800, castironpi wrote:

> Classes and modules are really similar. In Python they're really
> *really* similar.

Yes they are.

Both are namespaces. The very last line of the Zen of Python says:

>>> import this
...
Namespaces are one honking great idea -- let's do more of those!


http://en.wikipedia.org/wiki/Namespace

--
Steven

Ben Finney

unread,
Mar 5, 2008, 5:36:29 PM3/5/08
to
"D'Arcy J.M. Cain" <da...@druid.net> writes:

> On Wed, 5 Mar 2008 13:38:59 -0800 (PST)
> Mike Driscoll <kyos...@gmail.com> wrote:
> > On Mar 5, 12:50 pm, castiro...@gmail.com wrote:
>
> I'm not sure why you change the address like this but could you
> please include the full address.

It's a misfeature of Google's mail interface. The poster to whom
you're responding could choose a different mail interface, but you'll
have a difficult time convincing them of that.

> Those of us who identify the time wasters would also like to drop
> the responses to their posts and changing the address makes this
> impossible.

Not at all. AFAIK the messages from Google mail correctly include the
'In-Reply-To' field or the 'References' field in the message header.
So, you can know by those fields whether a message is part of a thread
you've previously identified.

--
\ "I wish there was a knob on the TV to turn up the intelligence. |
`\ There's a knob called 'brightness' but it doesn't work." -- |
_o__) Eugene P. Gallagher |
Ben Finney <b...@benfinney.id.au>

D'Arcy J.M. Cain

unread,
Mar 5, 2008, 8:07:33 PM3/5/08
to Mike Driscoll, pytho...@python.org
On Wed, 5 Mar 2008 14:00:17 -0800 (PST)
Mike Driscoll <kyos...@gmail.com> wrote:
> What are you talking about? I didn't change the address at all. I'm
> not even sure what you mean. Are you talking about the post subject
> line (which I have never touched in any post)? If you're talking about
> the OP's email address, that's Google's fault for cropping them.

I'm talking about castironpi. I find his posts a waste of my time so I
have them filtered out along with a few others and I also filter out
responses by searching for his address in the body so changing it
defeats that. However, if it is something that you have no control over
I apologize for the noise.

--

D'Arcy J.M. Cain

unread,
Mar 5, 2008, 8:11:20 PM3/5/08
to Ben Finney, pytho...@python.org
On Thu, 06 Mar 2008 09:36:29 +1100
Ben Finney <bignose+h...@benfinney.id.au> wrote:
> > Those of us who identify the time wasters would also like to drop
> > the responses to their posts and changing the address makes this
> > impossible.
>
> Not at all. AFAIK the messages from Google mail correctly include the
> 'In-Reply-To' field or the 'References' field in the message header.
> So, you can know by those fields whether a message is part of a thread
> you've previously identified.

I don't want to have to tag every thread. I just want to *plonk*
certain posters.

Anyway, I'll live with Google's failings I guess.

casti...@gmail.com

unread,
Mar 5, 2008, 9:06:23 PM3/5/08
to
On Mar 5, 4:25 pm, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com.au> wrote:
> On Wed, 05 Mar 2008 13:49:20 -0800, castironpi wrote:
> > Classes and modules are really similar.  In Python they're really
> > *really* similar.
>
> Yes they are.
>
> Both are namespaces. The very last line of the Zen of Python says:
> Namespaces are one honking great idea -- let's do more of those!

Where to begin?

casti...@gmail.com

unread,
Mar 6, 2008, 12:35:51 AM3/6/08
to

And white to play. What does exec( open( 'modA.py' ).read() ) do?

Daniel Fetchinson

unread,
Mar 6, 2008, 1:17:45 AM3/6/08
to casti...@gmail.com, pytho...@python.org
> > Where to begin?

>
> What does exec( open( 'modA.py' ).read() ) do?

The most appropriate list to ask those questions is:

http://mail.python.org/mailman/listinfo/tutor

Mike Driscoll

unread,
Mar 6, 2008, 9:20:03 AM3/6/08
to
On Mar 5, 7:07 pm, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Wed, 5 Mar 2008 14:00:17 -0800 (PST)
>
> Mike Driscoll <kyoso...@gmail.com> wrote:
> > What are you talking about? I didn't change the address at all. I'm
> > not even sure what you mean. Are you talking about the post subject
> > line (which I have never touched in any post)? If you're talking about
> > the OP's email address, that's Google's fault for cropping them.
>
> I'm talking about castironpi. I find his posts a waste of my time so I
> have them filtered out along with a few others and I also filter out
> responses by searching for his address in the body so changing it
> defeats that. However, if it is something that you have no control over
> I apologize for the noise.
>
> --
> D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolveshttp://www.druid.net/darcy/ | and a sheep voting on

> +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

No problem. I wish I could use something other than Google Groups when
I'm at work, but we currently have NNTP disabled...and I haven't found
a good workaround yet.

Mike

Carl Banks

unread,
Mar 6, 2008, 10:58:06 AM3/6/08
to
On Mar 5, 8:11 pm, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Thu, 06 Mar 2008 09:36:29 +1100
>
> Ben Finney <bignose+hates-s...@benfinney.id.au> wrote:
> > > Those of us who identify the time wasters would also like to drop
> > > the responses to their posts and changing the address makes this
> > > impossible.
>
> > Not at all. AFAIK the messages from Google mail correctly include the
> > 'In-Reply-To' field or the 'References' field in the message header.
> > So, you can know by those fields whether a message is part of a thread
> > you've previously identified.
>
> I don't want to have to tag every thread. I just want to *plonk*
> certain posters.
>
> Anyway, I'll live with Google's failings I guess.

Sounds like you need better filtering.

A decent news filter should be able not only kill a poster, but also
all followups to that poster recursively.

(No, I don't know of any that do that either.)


Carl Banks

Carl Banks

unread,
Mar 6, 2008, 11:00:58 AM3/6/08
to
On Mar 5, 8:07 pm, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Wed, 5 Mar 2008 14:00:17 -0800 (PST)
>
> Mike Driscoll <kyoso...@gmail.com> wrote:
> > What are you talking about? I didn't change the address at all. I'm
> > not even sure what you mean. Are you talking about the post subject
> > line (which I have never touched in any post)? If you're talking about
> > the OP's email address, that's Google's fault for cropping them.
>
> I'm talking about castironpi. I find his posts a waste of my time

"His" posts?


Carl Banks

Bruno Desthuilliers

unread,
Mar 6, 2008, 11:16:48 AM3/6/08
to
casti...@gmail.com a écrit :

> And white to play. What does exec( open( 'modA.py' ).read() ) do?

RTFM

casti...@gmail.com

unread,
Mar 6, 2008, 11:40:47 AM3/6/08
to
On Mar 6, 12:17 am, "Daniel Fetchinson" <fetchin...@googlemail.com>
wrote:

Thanks for the reference. I'm basically experienced with Python, but
I may read there too.

I asked the question mostly rhetorically. <simile> Like when people
ask, who cares, or who knows. They don't want the answer---- unless
there is one. </simile>

A more appropriate formulation of the 'question behind the words'
would have been, 'are there any weird corner cases in which it doesn't
import modA? I recognize it doesn't on .pyc and .pyd files, but you
could say exec( open( 'modA.py' ).read() ) ==> import modA, even if
import modA =!=> exec( open( 'modA.py' ).read() ) all the time.

However, a way of asking that's more direct would have been, "Wait...
can't you create multiple module instances like that?", but since
we've already seen successful loadings of at least the builtins,
that's been treated. Maybe you even hear the right thing if I say,
"Does exec( open( 'modA.py' ).read() ) do the things of import modA?"
Yes, I can read the docs, but no, I can't check every possible
combination of modules.

The idea behind a singleton is to ensure something. You want non-
primary instances impossible to create. However, if (wording got
tangled here) for singleton A, if class B does the same thing, just
has different allocations-- it's own-- then A is still a singleton.
The writer of B hasn't defeated or contradicted the letter or spirit
of A.

OP's question might have come from a variety of perspectives. In some
ways yes, in others no. That is, if you try to reinstantiate modA,
you get the same instance. But if you try really hard, you get a new
one. Are you looking for an extension to import modA that won't
return an instance that is old?

So far, I know this: modules and classes are both namespaces. Are
those singletons?

D'Arcy J.M. Cain

unread,
Mar 6, 2008, 11:41:58 AM3/6/08
to Carl Banks, pytho...@python.org
On Thu, 6 Mar 2008 08:00:58 -0800 (PST)
Carl Banks <pavlove...@gmail.com> wrote:
> > I'm talking about castironpi. I find his posts a waste of my time
>
> "His" posts?

Whatever. I'm too old to worry about searching for politically correct,
gender neutral pronouns.

D'Arcy J.M. Cain

unread,
Mar 6, 2008, 11:47:07 AM3/6/08
to Carl Banks, pytho...@python.org
On Thu, 6 Mar 2008 07:58:06 -0800 (PST)
Carl Banks <pavlove...@gmail.com> wrote:
> > I don't want to have to tag every thread. I just want to *plonk*
> > certain posters.
> >
> > Anyway, I'll live with Google's failings I guess.
>
> Sounds like you need better filtering.
>
> A decent news filter should be able not only kill a poster, but also
> all followups to that poster recursively.

Actually, I am reading this as a mailing list and since I run Unix and
procmail I am sure that I could set something up if it really starts to
be annoying. So far the simple filters deal with all but a smattering
of problem postings so I don't need to expend the time.

Carl Banks

unread,
Mar 6, 2008, 12:36:21 PM3/6/08
to
On Mar 6, 11:41 am, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Thu, 6 Mar 2008 08:00:58 -0800 (PST)
>
> Carl Banks <pavlovevide...@gmail.com> wrote:
> > > I'm talking about castironpi. I find his posts a waste of my time
>
> > "His" posts?
>
> Whatever. I'm too old to worry about searching for politically correct,
> gender neutral pronouns.


I'm pretty sure even the most PC people wouldn't suggest using a
masculine pronoun for an inanimate objects.


(Ok, some probably would.)

Carl Banks

casti...@gmail.com

unread,
Mar 6, 2008, 1:32:42 PM3/6/08
to
On Mar 6, 10:47 am, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Thu, 6 Mar 2008 07:58:06 -0800 (PST)
>
> Carl Banks <pavlovevide...@gmail.com> wrote:
> > > I don't want to have to tag every thread.  I just want to *plonk*
> > > certain posters.
>
> > > Anyway, I'll live with Google's failings I guess.
>
> > Sounds like you need better filtering.
>
> > A decent news filter should be able not only kill a poster, but also
> > all followups to that poster recursively.
>
> Actually, I am reading this as a mailing list and since I run Unix and
> procmail I am sure that I could set something up if it really starts to
> be annoying.  So far the simple filters deal with all but a smattering
> of problem postings so I don't need to expend the time.

The Google Groups reader doesn't always let you see new replies to
really old messages, something they should change (this means you).
But there is an 'active old threads', but it doesn't catch every one.
I want to read those plonk.

casti...@gmail.com

unread,
Mar 6, 2008, 1:35:02 PM3/6/08
to
> > > > I'm talking about castironpi.  I find his posts a waste of my time
>
> > > "His" posts?
>
> > Whatever.  I'm too old to worry about searching for politically correct,
> > gender neutral pronouns.
>
> I'm pretty sure even the most PC people wouldn't suggest using a
> masculine pronoun for an inanimate objects.
>
> (Ok, some probably would.)
>
> Carl Banks

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: Hey, that's not nice.
>>>

johan....@gmail.com

unread,
Mar 6, 2008, 2:03:24 PM3/6/08
to
On Mar 5, 7:50 pm, castiro...@gmail.com wrote:
> What is a class that is not a module?

A class is a bag of stuff and a namespace :)

J.

casti...@gmail.com

unread,
Mar 6, 2008, 2:37:50 PM3/6/08
to
On Mar 6, 1:03 pm, "johan.san...@gmail.com" <johan.san...@gmail.com>
wrote:

A module is a bag of stuff and a namespace. Different stuff.

{ '__module__', '__weakref__'}

Gabriel Genellina

unread,
Mar 6, 2008, 4:32:35 PM3/6/08
to pytho...@python.org
En Thu, 06 Mar 2008 14:40:47 -0200, <casti...@gmail.com> escribi�:

> On Mar 6, 12:17 am, "Daniel Fetchinson" <fetchin...@googlemail.com>
> wrote:
>>
>> > What does exec( open( 'modA.py' ).read() ) do?
>>
>

> A more appropriate formulation of the 'question behind the words'
> would have been, 'are there any weird corner cases in which it doesn't
> import modA? I recognize it doesn't on .pyc and .pyd files, but you
> could say exec( open( 'modA.py' ).read() ) ==> import modA, even if
> import modA =!=> exec( open( 'modA.py' ).read() ) all the time.

Then write the question that way. Working crystall balls are hard to find
nowadays...

They're different; no module object is created by the exec statement.
When the import machinery determines that certain module isn't already
loaded, and that the .py version has to be used, it does something like
this:

newmodule = sys.modules[modulename] = ModuleType(modulename)
# constructor sets __name__ and a null __doc__
newmodule.__builtins__ = current builtins
newmodule.__file__ = filename
code = read from filename and compile it
exec code in newmodule.__dict__

> However, a way of asking that's more direct would have been, "Wait...
> can't you create multiple module instances like that?", but since
> we've already seen successful loadings of at least the builtins,
> that's been treated.

There are ways to create many instances of the same module, but not using
a plain import statement, or using the API in the intended way.

> Maybe you even hear the right thing if I say,
> "Does exec( open( 'modA.py' ).read() ) do the things of import modA?"

Only in part, as you can see above.

> Yes, I can read the docs, but no, I can't check every possible
> combination of modules.
> The idea behind a singleton is to ensure something. You want non-
> primary instances impossible to create. However, if (wording got
> tangled here) for singleton A, if class B does the same thing, just
> has different allocations-- it's own-- then A is still a singleton.
> The writer of B hasn't defeated or contradicted the letter or spirit
> of A.
>
> OP's question might have come from a variety of perspectives. In some
> ways yes, in others no. That is, if you try to reinstantiate modA,
> you get the same instance. But if you try really hard, you get a new
> one. Are you looking for an extension to import modA that won't
> return an instance that is old?
>
> So far, I know this: modules and classes are both namespaces. Are
> those singletons?

Strictly speaking, neither classes nor modules are singletons; classes are
instances of type, and modules are instances of their type, and there are
many instances of each. We can consider "qualified singletons", where we
want to have a single instance of a class given certain attributes.

In that sense, modules are "named" singletons; Python tries to ensure
that, given a module name, the module object returned is always the same
one - even if you spell the name in different ways. The import statement,
the __import__ builtin, the imp module, the PyImport_AddModule C function,
all of them try to ensure that. Of course there are ways to shoot yourself
in the foot -by example, creating a new module "in the fly" with
PyModule_New-

On the contrary, classes have no such restrictions. Of course, for classes
defined at the global level in a module, you can't have two of them with
the same name, but that's just because the module namespace can't hold two
values for a single name. But you can create classes dinamically, or
define them inside a function, or... lots of ways of creating many
instances of the "same" class, and Python won't enforce them to be always
the same object.

So, I'd say that modules are named singletons, and classes aren't
singletons at all.

--
Gabriel Genellina

Gabriel Genellina

unread,
Mar 6, 2008, 4:32:35 PM3/6/08
to pytho...@python.org
En Thu, 06 Mar 2008 14:40:47 -0200, <casti...@gmail.com> escribi�:

> On Mar 6, 12:17 am, "Daniel Fetchinson" <fetchin...@googlemail.com>


> wrote:
>>
>> > What does exec( open( 'modA.py' ).read() ) do?
>>
>

> A more appropriate formulation of the 'question behind the words'
> would have been, 'are there any weird corner cases in which it doesn't
> import modA? I recognize it doesn't on .pyc and .pyd files, but you
> could say exec( open( 'modA.py' ).read() ) ==> import modA, even if
> import modA =!=> exec( open( 'modA.py' ).read() ) all the time.

Then write the question that way. Working crystall balls are hard to find
nowadays...

They're different; no module object is created by the exec statement.
When the import machinery determines that certain module isn't already
loaded, and that the .py version has to be used, it does something like
this:

newmodule = sys.modules[modulename] = ModuleType(modulename)
# constructor sets __name__ and a null __doc__
newmodule.__builtins__ = current builtins
newmodule.__file__ = filename
code = read from filename and compile it
exec code in newmodule.__dict__

> However, a way of asking that's more direct would have been, "Wait...


> can't you create multiple module instances like that?", but since
> we've already seen successful loadings of at least the builtins,
> that's been treated.

There are ways to create many instances of the same module, but not using

a plain import statement, or using the API in the intended way.

> Maybe you even hear the right thing if I say,


> "Does exec( open( 'modA.py' ).read() ) do the things of import modA?"

Only in part, as you can see above.

> Yes, I can read the docs, but no, I can't check every possible


> combination of modules.
> The idea behind a singleton is to ensure something. You want non-
> primary instances impossible to create. However, if (wording got
> tangled here) for singleton A, if class B does the same thing, just
> has different allocations-- it's own-- then A is still a singleton.
> The writer of B hasn't defeated or contradicted the letter or spirit
> of A.
>
> OP's question might have come from a variety of perspectives. In some
> ways yes, in others no. That is, if you try to reinstantiate modA,
> you get the same instance. But if you try really hard, you get a new
> one. Are you looking for an extension to import modA that won't
> return an instance that is old?
>
> So far, I know this: modules and classes are both namespaces. Are
> those singletons?

Strictly speaking, neither classes nor modules are singletons; classes are

Steven D'Aprano

unread,
Mar 7, 2008, 7:16:14 AM3/7/08
to
On Thu, 06 Mar 2008 08:40:47 -0800, castironpi wrote:

> you
> could say exec( open( 'modA.py' ).read() ) ==> import modA


Yes, you could say that, but you'd be wrong. Please test your code before
making such claims in the future.


--
Steven

casti...@gmail.com

unread,
Mar 8, 2008, 12:52:41 PM3/8/08
to
On Mar 7, 6:16 am, Steven D'Aprano <st...@REMOVE-THIS-

Aye aye. -1 response not phrased in the form of a question.

Is it correct that exec( open( 'modA.py' ).read() ) -and- from modA
import * create "effectively" same results, such as in the remaning
program not checking __module__ attributes?

Is there a slight modification of both sides that does cover a non-
trivial volume of programs, such as maybe,
exec( open( 'modA.py' ).read(), locals= dict( __module__= 'modA' ) ) -
and- from modA import *, or something? I notice that

from toimp import *
f()

exec( open( 'toimp.py' ).read() )
f()

generate the same output so far.

Steven D'Aprano

unread,
Mar 8, 2008, 7:32:03 PM3/8/08
to
On Sat, 08 Mar 2008 09:52:41 -0800, castironpi wrote:

> On Mar 7, 6:16 am, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Thu, 06 Mar 2008 08:40:47 -0800, castironpi wrote:
>> > you
>> > could say exec( open( 'modA.py' ).read() ) ==> import modA
>>
>> Yes, you could say that, but you'd be wrong. Please test your code
>> before making such claims in the future.
>
> Aye aye. -1 response not phrased in the form of a question.

All the words in that sentence are English, but as a whole it makes no
sense.


> Is it correct that exec( open( 'modA.py' ).read() ) -and- from modA
> import * create "effectively" same results,

You're getting closer now. (By the way, exec is a statement, so you don't
need brackets around its arguments.)

exec open('modA.py').read()

gives *almost* the same results as

from modA import *


The differences I can think of:

(1) The exec version doesn't cache the results, so if you execute the
line repeatedly you will execute the same code over and over again,
possibly causing unexpected side-effects. import caches the results, and
so will be faster.

(2) The import version may read from modA.pyc, modA.pyo or modA.so even
if there is a modA.py file. The exec version can't import from anything
but modA.py.

(3) The exec version as written always puts the results in the global
scope, even if you execute it inside a function. (In the future, "from
modA import *" will be forbidden inside functions, but at the moment it
is still legal.)

(4) The exec version doesn't search sys.path for modA; import does.

(5) The exec version can't read modules inside a zip file; import can.

There may be other differences I haven't thought of.

> such as in the remaning
> program not checking __module__ attributes?

Again, I don't understand what you are saying.


> Is there a slight modification of both sides that does cover a non-
> trivial volume of programs, such as maybe, exec( open( 'modA.py'
> ).read(), locals= dict( __module__= 'modA' ) ) - and- from modA import
> *, or something?


*Slight* modification, no.

I'm sure that with sufficient effort, you could add caching, search
sys.path, and look inside zip files. Those three things wouldn't be
horribly difficult, but it wouldn't be a one-liner -- off the top of my
head, I'd guess fifty to a hundred lines of code to re-invent those
particular wheels correctly.

However, recreating the rest of the import mechanism would be terribly
complicated. I'm not really sure it could be done. For example, how would
you deal with modules written in C from within Python? You'd need access
to Python internals that, as far as I know, aren't available.

--
Steven

0 new messages