[Tutor] calling a module fails
Dave Angel
d at davea.name
Tue Oct 30 02:24:26 CET 2012
On 10/29/2012 09:00 PM, richard kappler wrote:
> Methinks I'm missing something obvious, but can't quite put my finger on
> it. If, in the interpreter, I enter the following code:
>
> def hungry(batVolt):
> if batVolt >94:
> return ("I am not hungry at the moment")
> elif 64 < batVolt < 95:
> return ("I'm starting to get hungry")
> else:
> return ("I'm hungry!")
>
> and then run
>
> hungry(98)
>
> with 98 just being an example of the many numbers I tried when testing
> this, I get the return I expected and all is well.
>
> If, however, I save the above in a file named hungry.py, then import
> hungry, I get an error, as follows:
>
> import hungry
> hungry(96)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: 'module' object is not callable
>
> So what am I missing? Someone do please point out the obvious. ;-)
>
> regards, Richard
>
>
I'd recommend NOT ever calling the module by the same name as one of the
functions or classes in it.
When you want to call a function in an external module, you have a
choice of:
import mymodule
mymodule.myfunction(98)
or
from mymodule import myfunction
myfunction(98)
By using the same name, it isn't obvious to you that what you need is:
hungry.hungry(98)
--
DaveA
More information about the Tutor
mailing list