Page Contents (hide)
Python is an object-oriented language, but it provides both a class definition structure for object-oriented programming and standalone functions for procedural programming. In addition, the standard features of Python have been extended with the use of modules. In this chapter, we introduce the use of functions and modules in Python.
A function in Python is similar to a static
method in Java. That is, they are defined and used independent of an object. The main difference is that Python functions are not defined within a class.
Some functions are very common and are part of the Python language itself These built-in functions are considered to be global to the program. Thus, they are always available and can be invoked using only the function name.
Python provides a number of built-in functions which will be introduced and described throughout the book.
When passing parameters to a function, the reference to the object is passed and not the value itself. This is the expected behavior since Python represents all data as objects and all variables contain references.
Multiple parameters are separated with commas as in Java. If a function requires three arguments, then three values must be supplied. Python supports optional arguments which will be explored in a later section.
Function definitions do not include parameter types as is required in Java. The first argument in the function call is passed to the first parameter in the function definition, the second argument to the second parameter and so on.
The programmer must be responsible and pass the correct argument types expected by the function. Some functions have been defined to check the type of data passed as arguments and will raise an exception if an incorrect data values are provided.
![]() | Note:
Not having parameter types allows for the development of more powerful functions that can be used with many different data types. |
Some functions may return a single value. This value can be used or ignored depending on the purpose of the function. If a function returns a value, it will always be an object reference.
Python is a relatively small language but provides the necessary components for creating powerful programs. Additional functionality is available from various components in the Python Standard Library.
The Standard Library includes a number of functions and class definitions in which related components are organized into individual files called modules. A module is simply a Python source file containing various function and class definitions. These components can be used within a Python program via the import
statement. To include the math module you would use
at the top of your program file. While this appears to be similar to the Java statement
note that in Java, each class was stored in a separate file and a collection of classes comprised a package. The Java statement above makes all classes within the java.util package available for use in your Java program. The Python import
statement, includes the named module and makes all definitions within a single module avaiable for use in your Python program.
To use a module function or class, you must import the module as described above and then simply specify the function or class name
You can import individual components of a module and make them available within the current file using
This version includes the module, but only makes the sqrt()
function available for use in your program.
Python has a second variation to the import
statement which works very much like the from/import
version
but in this case, the definition are in a different namespace. For now, we will use the first version until we discuss namespaces in a later chapter.
The import
statement will be discussed in more detail later when dealing with variable scopes and user-defined modules.
The Python Standard Library contains a number of modules including some with function definitions only and others with class definitions. One of the more commonly used function only modules is described in the following section. A complete list of the standard modules is provided in the appendix.
The standard math
module provides a number of mathematical functions. This module is basically a wrapper around the Standard C math functions, though, some functions specific to Python are also included.
To use a function in the math
module, you must first import the module. Consider the following sample program.
The following is a list of the more commonly used mathematical functions. All parameters must be numeric and each function returns a floating point value unless otherwise indicated.
ceil( x )
floor( x )
sqrt( x )
cos( x )
, sin( x )
, tan( x )
degrees( x )
radians( x )
A class is used to define a class of objects and objects are created or instantiated from a class. Once an object has been created, it can then be used. In Python, all literal values result in the creation of objects.
To instantiate an object directly from the class, the class constructor is called as if it were a standalone function
In Java, the new operator was used to instantiate objects as illustrated below
The Python date
class is defined in the standard datetime
module. In order to create objects from the class, it must first be imported. The from
version of the import statement
is commonly used to import individual classes as is done here for the date
class. If you use the plain import statement, the class name would remain part of the datetime()
module and the module name would have to be prepended to all references of the class
Once an object has been created, it can then be used via one of its methods. The dot operator is used to select a particular method as is done in Java. In the following example,
returns the day of the week for the given date. The rules for passing arguments and using the return value, if any, are the same as for standalone functions.
![]() | Note:
Python built-in types have various operators defined that can be used with objects of those types. An example of which are the mathemaical operators defined for the numeric types. Not all operators can be used with every type. But these operators are considered methods of the various classes. |
The numeric primitive types each have a built-in constructor for creating the various objects. These constructors can also be used to convert between the various types.
complex( x, y )
float( x )
int( x )
long( x )