15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

11.6. Variable-Length Arguments<br />

There may be situations where your function is required to process an unknown number of arguments.<br />

These are called variable-length argument lists. Variable-length arguments are not named explicitly in<br />

function declarations because the number of arguments is unknown before runtime (and even during<br />

execution, the number of arguments may be different on successive calls), an obvious difference from<br />

formal arguments (positional and default), which are named in function declarations. <strong>Python</strong> supports<br />

variable-length arguments in two ways because function calls provide for both keyword and nonkeyword<br />

argument types.<br />

In Section 11.2.4, we looked at how you can use the * and ** characters in function calls to specify<br />

grouped sets of arguments, non-keyword and keyword arguments. In this section, we will see the same<br />

symbols again, but this time in function declarations, to signify the receipt of such arguments when<br />

functions are called. This syntax allows functions to accept more than just the declared formal<br />

arguments as defined in the function declaration.<br />

11.6.1. Non-keyword Variable Arguments (Tuple)<br />

When a function is invoked, all formal (required and default) arguments are assigned to their<br />

corresponding local variables as given in the function declaration. The remaining non-keyword variable<br />

arguments are inserted in order into a tuple for access. Perhaps you are familiar with "varargs" in C (i.<br />

e., va_list, va_arg, and the ellipsis [ ... ]). <strong>Python</strong> provides equivalent supportiterating over the tuple<br />

elements is the same as using va_arg in C. For those who are not familiar with C or varargs, they just<br />

represent the syntax for accepting a variable (not fixed) number of arguments passed in a function call.<br />

The variable-length argument tuple must follow all positional and default parameters, and the general<br />

syntax for functions with tuple or non-keyword variable arguments is as follows:<br />

def function_name([formal_args,] *vargs_tuple):<br />

"function_documentation_string"<br />

function_body_suite<br />

The asterisk operator ( * ) is placed in front of the variable that will hold all remaining arguments once<br />

all the formal parameters if have been exhausted. The tuple is empty if there are no additional<br />

arguments given.<br />

As we saw earlier, a TypeError exception is generated whenever an incorrect number of arguments is<br />

given in the function invocation. By adding a variable argument list variable at the end, we can handle<br />

the situation when more than enough arguments are passed to the function because all the extra (nonkeyword)<br />

ones will be added to the variable argument tuple. (Extra keyword arguments require a<br />

keyword variable argument parameter [see the next section].)<br />

As expected, all formal arguments must precede informal arguments for the same reason that positional<br />

arguments must come before keyword arguments.<br />

def tupleVarArgs(arg1, arg2='defaultB', *theRest):<br />

'display regular args and non-keyword variable args'

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!