[Tutor] Files and Modules
Carroll, Barry
Barry.Carroll at psc.com
Fri Jun 9 19:33:24 CEST 2006
Kermit:
> ------------------------------
>
> Message: 7
> Date: Fri, 09 Jun 2006 11:02:44 -0400
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] file attribute of module
> Cc: tutor at python.org
> Message-ID: <44898D94.4030405 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Kermit Rose wrote:
> >>>> factor30.__file__
> > 'c:\\math\\factoring\\factor30.py'
> >
> > Ok. Now I understand it.
> >
> > The module is named factor30.
> >
> > The file is named factor30.py
> >
> > I presume that if I created another library in the same directory
> > in a file named factor31.py
> > that it would create a module named factor31. ???
>
> Yes. Try it and see!
>
> Kent
>
If by "it" you mean "Python", you are correct. As you are finding out,
the distinction between 'file' and 'module' in Python is subtle but
important. The words are often used synonymously, but they actually
have very different meanings. Here is the best short description I have
found of the distinction. It is from
Learning Python, 2nd Edition
By David Ascher, Mark Lutz
Publisher: O'Reilly
Modules are probably best understood as simply packages
of
names-places to define names you want to make visible to
the
rest of a system. In Python, modules are a namespace-a
place
where names are created. Names that live in a module are
called
its attributes. Technically, modules usually correspond
to
files, and Python creates a module object to contain all
the
names assigned in the file; but in simple terms, modules
are
just namespaces.
So, in your example above, Python uses the contents of the file
"factor31.py" to create the module "factor31".
As others have hinted, Python can use things other than files, and types
of files other than "???.py", to make a module, but you don't need to
worry about that yet.
HTH.
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
More information about the Tutor
mailing list