3

Sorry for the very silly question. I am a self-study beginner in python and I am having issues with using a function and calling it. I am coming from a MATLAB background so i was trying to do something similar.

Tools used: Python 2 in a Linux environment

As a test, I created a function that i called prthis (for "print this") within a file called also prthis.py. This function just takes a number as an input, and then outputs two numbers, respectively the same one and its square. I defined it like this:

#----------------------------------------
# content of the file prthis.py
#----------------------------------------

def prthis(x):

    y=x*x

    nb=x 

    return (y, nb)
#------------------------------------------

then, within the python prompt, I try to call the newly created prthis function, and I do this:

>>> import prthis

>>> g,t = prthis(7)

The import seems to be succesful, but when I try the function on two outputs variable called g and t , like above, i get the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

Perhaps I am too much MATLAB-izing my thinking. Does anyone has a suggestion on how to deal with this?

PS: it's my first question ever on stackexchange, so please could you let me know how to thank/accept valuable answers from other users? I do not wish to look like ungrateful to those who would try to help.

sco1
  • 12,154
  • 5
  • 26
  • 48
brasamical
  • 93
  • 7
  • 2
    When you use `import [module]` you still need to use the module's name when calling its functions - try `g,t = prthis.prthis(7)` – asongtoruin Oct 28 '16 at 10:18
  • 2
    You are trying to call the _module_. You need to do `prthis.prthis(7)` _or_ instead of doing `import prthis`, do `from prthis import prthis` – Farhan.K Oct 28 '16 at 10:18
  • 1
    Here is a guide on [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Farhan.K Oct 28 '16 at 10:21
  • 1
    If you named your function something else, for example, `printthis`, you would call it by doing `prthis.printthis(7)` or doing `from prthis import printthis`. If you choose the second method, you will only be able to use the function `printhis` from `prthis.py`, no other functions or classes will be imported. – Farhan.K Oct 28 '16 at 10:23

2 Answers2

7

you are importing a module, not the function. If you want to import just the function you could do this:

from prthis import prthis
g,t = prthis(7)

but if you import the full module you have to define the module you are calling the function from as well:

import prthis
g,t = prthis.prthis(7)
Christian W.
  • 2,532
  • 1
  • 19
  • 31
-4

you are successfully able to import prthis but this is not the correct way either you should try "from prthis import prthis. Refer this for a better understanding of calling a function. What does it mean to "call" a function in Python?

Community
  • 1
  • 1
khushbu
  • 158
  • 11