Fundamentals » Functions and Methods »
Functions and Methods
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
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.
# Compute the absolute value of the integer x
y = abs( x )
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.
# Create a complex number with real value r and imaginary i.
z = complex( r, i )
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.
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
from math import *
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
To use a module function or class, you must import the module as described above and then simply specify the function or class name
from math import *
y = sqrt( x )
You can import individual components of a module and make them available within the current file using
from math import sqrt
This version includes the module, but only makes the
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 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.
To use a function in the
# Prompts for a real value and computes its square root.
from math import *
x = float( raw_input( "Enter a real value:" ) )
y = sqrt( x )
print "The square root of", x, "is", y
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.
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
today = date( 2006, 8, 25 )
In Java, the new operator was used to instantiate objects as illustrated below
// Java object creation
Date today = new Date( 2006, 8, 25 );
from datetime import date
is commonly used to import individual classes as is done here for the
tomorrow = datetime.date( 2006, 8, 26 )
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,
whichDay = today.weekday()
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.
Numeric Type Constructors
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.
© 2006 - 2008: Rance Necaise - Page last modified on September 15, 2006, at 03:10 PM