Chapter 9 Lists
9.5 Lists and Function Arguments¶
Like other objects, list objects can be passed as arguments to functions, or taken as return values. In the example below, the function duplicate takes a list argument and modifies it by duplicating its content.
In the example above, the global list l1 is passed as an argument to the function duplicate. When the function call is evaluated, the local argument l is assigned the value of l1. As a result, l and l1 are bound to the same list object. When l is modified by calling the extend method of l, l1 is modified simultaneously.
The program below, however, does not change the global variable l1.
The example above contains a common programming mistake when working with lists. The reason that the global vairable l1 is not modified is that, the assignment statement l = l + l does not modify the object l 1. When the function call is evaluated, the local parameter l is first assigned the value of l1, and bound to the same object. However, when the statement l = l + l is executed, the local identifier l is assigned to a new object, which is the concatenated list l + l = l1 + l1. After the assignment statement is executed, the local variable l and the global variable l1 are bound to two different objects, without l1 being modified. It is worth remembering that the only ways to modify a list object include the setitem, setslice, delitem and delslice operations, and calls to list modification methods such as append, extend and remove. The assignment statement changes the binding table but does not modify objects.
List parameters. Python allows the definition of a function that can take an arbitrary number of optional arguments. This is convenient when one does not know how many argument will be specified in a function call, but needs the function to handle a variable number of arguments. This mechanism is allowed by defining a special list parameter with a preceding * symbol. For example, consider extending the function sum which takes two or three arguments and returns their sum.
The function sum above takes two or more arguments, putting any argument beyond the second argument into a list, and assigning the list to the parameter z. The function body performs standard summation, with the sum s being initialized to the value x + y, and updated incrementally with each element in z.
At most one special list parameter can be put into the definition of a function, and it must be put after all normal parameters (including those with default valves). In a function call, all the normal and default arguments are assigned their values first, with any additional arguments specified in the function call being put into a list in their order, and then assigned to the special list argument. A special list argument can be passed on from one function to another. For example:
In the example above, f calls g with y, passing all the extra arguments to g. In g, these arguments are put into the list x, and the first is returned. In fact, any list can be passed as arguments in function calls. By adding a preceding *, a list is expanded to fill individual arguments.
In the example above, the list l is expanded in the function call f(∗l), with its two elements being assigned to x and y, respectively.
© Copyright 2024 GS Ng.