beginner import confusion
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Apr 3 16:13:02 EST 2004
DilbertFan wrote:
> Hi,
> I really thought that I had this importing business down.
> I recently downloaded and installed mx module for the DateTime class.
>
> In IDLE, I go:
> import mx
> mx.DateTime.DateTime(2004)
> I get AttributeError: 'module' object has no attribute 'DateTime'
mx is a package, mx.DateTime is a module; you need to import a module
to get to its contents.
> but if you type:
> import mx.DateTime
> mx.DateTime.DateTime(2004)
> <DateTime object for '2004-01-01 00:00:00.00' at a979e0>
> If you "import mx", ... doesn't that get everything?
If mx were a module, you would be right. However, it is a "package,"
a collection of modules and packages. Packages are a way of keeping
the names one provider of many python modules from conflicting with
the module names of both you and python itself.
> If you know, from the docs, that it contains a DateTime class
> and then a DateTime object,
mx.DateTime.DateTime is a class, not an object.
You could do:
from mx.DateTime import DateTime
to simply get the DateTime class and then use DateTime(2004).
--
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list