module confusion

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Fri Oct 5 02:51:05 EDT 2007


In message <mailman.1557.1191561184.2658.python-list at python.org>, Robert
Kern wrote:

> Lawrence D'Oliveiro wrote:
>> In message <13gasot6r5bnu1a at corp.supernews.com>, Steven D'Aprano wrote:
>> 
>>> What does type(os.path) return when you try it?
>> 
>> It returns the type of the value contained in that variable, of course:
>> 
>>     >>> import os
>>     >>> os.path = 3
>>     >>> type(os.path)
>>     <type 'int'>
>> 
>> See, it's just a variable, like any other.
> 
> Oooookay. No one is contending that the "os.path" name can't be reassigned
> to a different object or that the "os.path" name is somehow different from
> any other name in Python. It's not wrong to say that "os is a module"
> either, even though you can obviously reassign that name to another
> object, too.

It is not the _name_ that is being reassigned, it is the _variable_ that the
name is bound to. All names in Python are bound to variables at all times.

> What I meant when I said "os.path is a bit of a weird case" is that, by
> default, the object referred to by the name "os.path" (assuming you've
> imported the standard library's os module) is another module and that os
> itself is a module, not a package like logging is. This is somewhat odd,
> because most modules aren't exposed that way. They are either in their own
> file and accessed by importing them directly, or they are inside a
> package.

That is also true of the module pointed to by os.path. Like any other
non-built-in module, it lives in its own file.

It also helps to keep clear the difference between a "module" and a "module
object". A "module" is the contents of a Python source file, while
a "module object" is a type of in-memory Python object. An "import"
statement generates the latter from the former. See
<http://docs.python.org/ref/types.html>. Admittedly, other parts of the
Python docs do not keep the distinction clear, but the section on "Modules"
in that page does just about do so.

You can't have modules within modules. But there's no reason an attribute of
one module object can't point to another module object. Notice I say "point
to" rather than "contain". There is no sense in which any Python object
can "contain" any other.



More information about the Python-list mailing list