module confusion

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Oct 3 12:56:09 CEST 2007


Lawrence D'Oliveiro a écrit :
> In message <mailman.1404.1191345163.2658.python-list at python.org>, Robert
> Kern wrote:
> 
>> Lawrence D'Oliveiro wrote:
>>
>>> In message <mailman.1379.1191301581.2658.python-list at python.org>, Robert
>>> Kern wrote:
>>>
>>>> Not all of the modules in a package are imported by importing the
>>>> top-level package.
>>> You can't import packages, only modules.
>>>
>>>> os.path is a particularly weird case because it is just an alias to the
>>>> platform-specific path-handling module; os is not a package.
>>> os is a module, os.path is a variable within that module. That's all
>>> there is to it.
>> Yes, but os.path is also module. That's why I said it was a weird case.
> 
> You can't have modules within modules. 

If you're talking about the filesystem representation (ie : .py files), 
you obviously can't have a file within a file, indeed.

When it comes to the internal runtime representation of modules in 
Python, then it's totally different - a module is just an object, that 
can of course be an attribute of another module object.

 >>> import os
 >>> type(os)
<type 'module'>
 >>> type(os.path)
<type 'module'>
 >>>

> os.path isn't an exception--see
> below.
> 
>> In [1]: import os
>>
>> In [2]: type(os.path)
>> Out[2]: <type 'module'>
> 
> On my Gentoo system:
> 
>     >>> import os
>     >>> os.path
>     <module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>
> 
> It's just a variable that happens to point to the posixpath module.

It's just a name bound to a module object.



More information about the Python-list mailing list