Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Parameter Passing

Parameter passing methods  are the ways in which parameters are transfered between methods when one method calls another. Python provides only one parameter passing method--pass-by-reference .

Consider the pair of Python methods defined in Program gif. On line 4, the method one calls the method two. In general, every method call includes a (possibly empty) list of arguments. The arguments specified in a method call are called actual parameters . In this case, there is only one actual parameter--x.

   program56900
Program: Example of parameter passing.

On line 7 the method two is defined as accepting a single argument y. The arguments which appear in a method definition are called formal parameters .

The semantics of pass-by-reference work like this: The effect of the formal parameter definition is to create a name in the local namespace of the function and then to bind that name to the object named by the actual parameter. For example, in the method two the formal parameter is called y. When the method is called, the name y is assigned to the object named x.

Since the formal parameters give rise to names in the local namespace, when a formal parameter is assigned a new object is bound to that name in the local namespace. The object named by the actual parameter is no longer accessible.

The output produced when the method one defined in Program gif is called is:

1
1
2
1


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.