My own list object

Delaney, Timothy tdelaney at avaya.com
Tue Feb 5 17:45:38 EST 2002


> I made a file mylist.py
> 
> # mylist.py
> class MyList:
>     def __init__(self):
>         self.list=[]
>     def append(self, item):
>         self.list.append(item)
> 
> >>> import mylist
> >>> x = mylist()
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: object of type 'module' is not call
> 
> Why is this happening?  My list object must be a class in its own,
> importable file.

That statement is incorrect. Read up on Python classes and modules.

Basically, a module contains definitions of things, including classes. You
can have as many definitions as you want in a module. You can also have code
which executes when the module is imported.

A module is also a namespace - to get at the definitions within that
namespace, you have to qualify them somehow.

With your example, you import the module mylist (thus binding the name
'mylist' to the module object). You then try to use the mylist module as a
callable - which it is not. However, in the mylist namespace is an object
named 'MyList' - which is your class definition.

So the call you need to make is

    x = mylist.MyList()

You should now got to http://www.python.org/doc/current/tut/tut.html and
work your way through the tutorial. If you have further questions, the FAQ
http://www.python.org/cgi-bin/faqw.py should be able to help you.

Tim Delaney




More information about the Python-list mailing list