Storing Functions using the Raspberry Pi 3

Storing functions

Don't-Forget-New

A module need not include a line to locate the interpreter, as that will be included in the program file.

Python function definitions can usefully be stored in one or more separate files for easier maintenance, and to allow them to be used in several programs without copying the definitions into each one. Each file storing function definitions is called a “module” and the module name is the file name without the “.py” extension.

Functions stored in the module are made available to a program using the Python import keyword followed by the module name. Although not essential, it is customary to put any import statements at the beginning of the program. Imported functions can be called using their name dot-suffixed after the module name, e.g. a “steps” function from an imported module named “ineasy” can be called with ineasy.steps().

Where functions stored in a module include parameters, it is often useful to assign a default argument value to the parameter in the definition. This makes the function more versatile, as it becomes optional for the call to specify an argument value:

 

cat.py

Step 1
Launch a plain text editor, then begin a Python module with a function definition that supplies a default string value to its argument
def purr( pet = ‘A cat’ ) :
print( pet + ‘ says MEOW!’ )

Step 2
Next, add two more function definitions that also supply default string argument values to their parameters
def lick( pet = ‘A cat’ ) :
print( pet + ‘ drinks milk’ )
def nap( pet = ‘A cat’ ) :
print( pet + ‘ sleeps by the fire’ )

Step 3
Now, save the file as “cat.py” so the module is named “cat”

There is no need to make the module file executable itself, as it is merely a library containing definitions that can be imported into an executable program. It should be placed in the same directory as the program files so the interpreter can easily find it. Optionally, you can create an alias when importing a module
using import as keywords. For example, import RPi.GPIO as GPIO allows you to simply use GPIO as the function prefix in calls.

kitty.py

 

Step 4
Launch a plain text editor and begin a new Python program by locating the interpreter
#! /usr/bin/env python

Step 5
Add a statement to make the module functions available
import cat

Step 6
Next, call each function without supplying an argument
cat.purr() ; cat.lick() ; cat.nap()

Step 7
Now, call each function again and pass an argument to each, then save the file
cat.purr( ‘Kitty’ ) ; cat.lick( ‘Kitty’ ) ; cat.nap( ‘Kitty’ )

tiger.py

Step 8
Begin another program by locating the interpreter and making the module functions available once more
#! /usr/bin/env python
import cat

Step 9
Request the user enters a name, then call each function passing the user-defined value as the argument
pet = raw_input( ‘Enter A Pet Name: ‘ )
cat.purr( pet ) ; cat.lick( pet ) ; cat.nap( pet )

Step 10
Finally, save the file and make both files executable with chmod, then run the programs to see the functions called

 

 

 

 

 

 

 

 

 

Hot-Tip-New_cropped

 

Notice how multiple statements can appear on the same line if separated by a ; semicolon character.

 

 

 

 

 

 

Raspberry Pi 3 in easy steps 9781840787290

Want to know more?

For the complete Raspberry Pi 3 guide, all in the trusted In Easy Steps style, click here. In full-colour and straightforward, jargon-free language, Raspberry Pi 3 in Easy Steps enables complete beginners to create their very own computer programs with the Scratch visual programming environment. It also instructs programming in the high-level (human-readable) Python programming language, which is processed by the Python ‘interpreter’ to produce results fast.