Can't instantiate class

Fredrik Lundh fredrik at pythonware.com
Sun Nov 6 09:38:58 EST 2005


David Mitchell wrote:

> Here is a very basic question, but it is frustrating me to no end
> nonetheless.
>
> I have one file called addLink.py. In a method in this file I am trying
> to instantiate a class and call a method from that class. Here is the code:
>
> def getCategories():
> # instantiate the DataUtil class
> db = DataUtil()
> # and call the getConnection() method
> connection = db.getConnection()
>
> ...
>
> At the top of this file I am importing the DataUtil module (named
> DataUtil.py) with this line:
>
> import DataUtil

> When I execute the getCategories() method above I get the following error:
>
>   File "C:\Apache2\htdocs\Intranet\addLink.py", line 42, in getCategories
>     db = DataUtil()
>
> TypeError: 'module' object is not callable
>
> Any idea what I'm doing wrong?

the error message contains the full story: when you do "import DataUtil",
you get an object named "DataUtil" that represents the module.  (import
is not an include statement).  and modules are not callable.

to access the DataUtil object *inside* the DataUtil module, use

    import DataUtil

    db = DataUtil.DataUtil()

or

    from DataUtil import DataUtil

    db = DataUtil()

also see:

    http://effbot.org/zone/import-confusion.htm

</F> 






More information about the Python-list mailing list