Basic Question

Henrik Motakef henrik.motakef at web.de
Fri Feb 15 14:03:53 EST 2002


Karthikesh Raju <karthik at james.hut.fi> writes:

> import random
> 
> def randomList(n):
>     s = [0] * n
>     for i in range(n):
>         s[i] = random.random()
>     return s
> 
> in a file called randomList.py. Started python, imported randomList
> (>> import randomList) and then called this function (>>
> randomList(5)), got this un-understandable error:
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: object of type 'module' is not callable

With the plain "import", you just loaded the module called randomList
(after the file it lives in). This module coutains the function you
def's. So you either have to write

>>> import randomList
>>> randomList.randomList(5)

or

>>> from randomList import randomList
>>> randomList(5)

hth
Henrik



More information about the Python-list mailing list