The real value of Python does not lie in its usefulness as a command-line calculator. Instead, it is because it is a full-featured programming language. The point of this is that you and others can write code blocks that can be reused - write once, use forever. We have already created one function from the command line on the "Basics" page. However, this approach to writing functions still forces us to type the function anew in each Python session. Instead, we will often want to type our more complicated functions into separate files which we can then run from the Python command line, or even by themselves.
Consider the following exceptionally simple function. We write it using multiple lines just to remind you that indentations are important.
def f(x): y = 4+x*(-3.0+x*(1.7+x)) return y
We can type this function into a file using our favorite text editor. We'll call the file myfunctions.py. Observe that the .py extension is important - Python assumes that files to be imported have that extension, so if you use something else or have no extension, it will probably cause problems. Python files end in ".py".
Once we have the function saved in a file, we can import it into Python in the same way we imported Numpy. Start Python from the directory where that file is located, and type the following.
>>> from myfunctions import * >>> f(4) 16
As with Numpy itself, we could also import the file as its own object, and then use the name of that object in referring to the functions it contains. There are a few advantages to that in this case. For one thing, you no longer have to worry about replacing an existing Python function with your own - now your function name has e.g. "mf." prepended. Moreover, chances are that you are working on your functions and debugging them, so you are probably making changes to your file of functions constantly. In that case, you always want to run the most recent version in Python. Python does import the class anew if you use the import statement, but you can use the reload command to get your revised functions.
>>> import myfunctions as mf >>> mf.f(4) 16 >>> reload(mf) <module 'myfunctions' from 'myfunctions.py'>
If you have used Matlab, you know that one annoying thing about it is that functions must always be grouped one to a file, with the filename the same that of the function it contains. That is not true in Python. We can put as many individual functions as we want in the file myfunctions.py. Note, however, that if we want to use Numpy functions in the functions we write ourselves we must import it in the file containing our functions. Here is our new myfunctions.py file.
from numpy import * def f(x): y = 4+x*(-3.0+x*(1.7+x)) return y def g(x): return sin(pi*x)+4.0*x*x
Now we can reload our file in Python, and then use the new function.
>>> reload(mf) <module 'myfunctions' from 'myfunctions.py'> >>> mf.g(1.5) 8.0
We note in passing that Python is an object oriented language. We can create classes and instance them, and do all the sophisticated things that modern programmers expect. However, this is beyond the scope of the current discussion - we will touch on it briefly later, but not with any intention to learn object oriented programming. Our goal here is much more limited.
There is another possibility in Python. We can actually run our Python code without ever starting the Python interpreter. In this case our Python function essentially becomes a command. We can run the program from the command line by typing Python myfunctions.py. When we run Python with a file name after it, then it assumes that we are to run the commands in the file and then quit.
If we are just going to run the file, we need some top-level statements to execute. Otherwise the file only defines a couple of functions without using them. We change the file as follows.
from numpy import * def f(x): y = 4+x*(-3.0+x*(1.7+x)) return y def g(x): return sin(pi*x)+4.0*x*x y=f(g(3.0)) print "f(g(3.0)) = "+str(y)
The print statement makes sure that the function prints out an answer. Otherwise it would just compute f(g(3.0)), assign the value to a variable y, and quit. This way we get to see the answer. Thus, we defined two functions f and g, then told Python to compute f(g(3.0)) and assign that number to a variable called y, and finally to print out a message saying that "f(g(3.0)) = " and append to that a string (characters) representing the value of y. When we run this, we see
Python myfunctions.py f(g(3.0)) = 1296.0