Yes, but in what file is pyth_test
's definition declared in? Is it also located before it's called?
Edit:
To put it into perspective, create a file called test.py
with the following contents:
def pyth_test (x1, x2):
print x1 + x2
pyth_test(1,2)
Now run the following command:
python test.py
You should see the output you desire. Now if you are in an interactive session, it should go like this:
>>> def pyth_test (x1, x2):
... print x1 + x2
...
>>> pyth_test(1,2)
3
>>>
I hope this explains how the declaration works.
To give you an idea of how the layout works, we'll create a few files. Create a new empty folder to keep things clean with the following:
myfunction.py
def pyth_test (x1, x2):
print x1 + x2
program.py
#!/usr/bin/python
# Our function is pulled in here
from myfunction import pyth_test
pyth_test(1,2)
Now if you run:
python program.py
It will print out 3. Now to explain what went wrong, let's modify our program this way:
# Python: Huh? where's pyth_test?
# You say it's down there, but I haven't gotten there yet!
pyth_test(1,2)
# Our function is pulled in here
from myfunction import pyth_test
Now let's see what happens:
$ python program.py
Traceback (most recent call last):
File "program.py", line 3, in <module>
pyth_test(1,2)
NameError: name 'pyth_test' is not defined
As noted, python cannot find the module for the reasons outlined above. For that reason, you should keep your declarations at top.
Now then, if we run the interactive python session:
>>> from myfunction import pyth_test
>>> pyth_test(1,2)
3
The same process applies. Now, package importing isn't all that simple, so I recommend you look into how modules work with Python. I hope this helps and good luck with your learnings!