Page Contents (hide)
Functions in Python are equivalent to static or class methods in Java. The only difference is that they are not defined as part of a class. Instead, they are defined at the global-level and referenced by name alone or prefixed with a module name if defined within a module.
Python allows users to create user-defined functions that can be used to construct procedural programs. Functions allow for the subdivision of larger problems into smaller parts and efficient code reuse. In this chapter, we cover the creation of user-defined functions and assume the reader has experience writing static Java methods.
A Python function contains a header and body. The function header is specified using the def
keyword while the function body is specified as a statement block. The following function definition
sums the range of values specified by the first and last arguments. A similar static method in Java would be written as
A user-defined function is called like any other function in Python as illustrated in the following code segment
Arguments in Python are passed by value and in the order specified. Since every value is an object, these values are always object references. The formal arguments defined for a function become aliases of the actual parameters passed to the function which it’s called.
Passing the reference to a mutable argument to a function allows that object to be modified by the function. Immutable objects can not be modified within a function and is similar to passing Java primitive types “by value”.
Python is a dynamically typed language with polymorphism everywhere. In fact, every operator in Python is a polymorphic operation. Polymorphism also plays a role with function arguments.
As you have noticed, data types are not specified for function arguments. So, what keeps us from passing floating-point values into the sumRange()
function? Consider the following example
which is a valid statement call in Python. So long as all operations within the function can be applied to the given values, the program will execute correctly. If an operation can not be applied to a given argument type, an exception will be raised to indicate the invalid type.
This flexability is a powerful feature of Python. It allows a single function to be applied to different object types without having to define multiple versions. To achieve the same flexability in Java, you would have to define the class and methods using generics which were introduced starting with Java version 1.5.
Python allows functions to be defined with default argument values. For example, we can add a third argument to the sumRange()
function to specify the step value.
We have also provided a default value for the step argument. If the value of the third argument is omitted when calling the function as with the original function
the default value is assigned to the formal argument before the function is executed. If the value is supplied in the function call,
that value is used instead. When definining functions with default arguments, you can not skip arguments. All arguments following the first one with a default value, must be assigned default valus. Otherwise, Python would have no way of knowing which argument is suppose to receive the default value. The function definition would be illegal since the second argument has a default value, but not the third one
As in Java and most other languages, Python passes arguments to functions in the order they were specified. But Python also allows you to specify the argument order by using keyword arguments. Consider the following function call
in which we directly specify which argument is suppose to receive which value.
All functions in Python return a value whether you specify one or not. The return
statement is used to terminate a function and return a value or more specifically to return an object reference. If you omit the return statement, Python automatically returns a reference to the None
object.
Most functions return a single value, but you can define functions that return multiple values by returning those values within a tuple. The use of tuples is discussed in a later chapter.
While the def
keyword is used to define a Python function, it is actually a statement which is executed by the interpreter. When Python executes a def
statement, a function object is created containing the statments within the body of the function. The function name is actually a variable to which the function object is assigned as illustrated in the following diagram
The statements within a function are not executed until the function is called even though the statements are interpreted an converted to byte-code as they are read. Therefore, you can refer to one function within another before the former has been defined. Consider the following code segment
This is not the case, however, when calling a function from a statement at file level. For example, the function call in the following code segment is invalid because the interpreter does not know about the function at the time the function call is executed
The order in which functions are listed within a file is not important, but the order of executable statements at file level is. Some people find it helpful to place the top-level statmentes within a single driver function and then call this function at the end of the file. Other functions used to subdivide the problem would be placed between the driver function and the call at the end of the file as illustrated in the following example.
Variables are classified by their scope which indicates where the variable was created and where it can be used. Python has four scope classifications:
Variables only exist within the scope in which they were created. The built-in and global scopes exist during the entire life of the program. Variables in the instance scope exist during the lifetime of the object for which they were defined. A local variable, however, only exist during the time in which the function is being executed. Each execution of a function, creates a new local scope which is then destroyed when the function terminates.
Global variables can be referenced within functions but local variables can not be referenced outside of their scope. Consider the following code segment
varA
and one
are global variables since they are declared at the top-most or file level. varB
and varC
are local to the function and only exist when the function is executed. It is legal to reference variable varA
from within one()
since varA
is a global variable. But what happens if we modify the function and assign a new value to varA
?
A variable in Python can not be modified outside of the scope in which it was declared. Thus, the assignment of zero to varA
within the function creates a new local variable with the same name as the global variable. The global variable varA
is not modified, but it is used in the summation statement since that statement is executed before the local variable varA
is created. The following diagram illustrates the variable statement at the end of the function but before it returns
To modify a global variable from within a function, you must use the global
declaration which tells the Python interpreter to modify the global variable when it is assigned a new value instead of creating a new local variable.
You can use a single global
declaration to flag multiple variables, each separated with a comma