12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Defaults: specify values for arguments that aren’t passedFunctions can specify default values for arguments to receive if the call passestoo few values, again using the name=value syntax.Varargs: collect arbitrarily many positional or keyword argumentsFunctions can use special arguments preceded with * characters to collect anarbitrary number of extra arguments (this feature is often referred to as varargs,after the varargs feature in the C language, which also supports variable-lengthargument lists).Varargs: pass arbitrarily many positional or keyword argumentsCallers can also use the * syntax to unpack argument collections into discrete,separate arguments. This is the inverse of a * in a function header—in the headerit means collect arbitrarily many arguments, while in the call it means pass arbitrarilymany arguments.Table 16-1 summarizes the syntax that invokes the special matching modes.Table 16-1. Function argument-matching formsSyntax Location Interpretationfunc(value) Caller Normal argument: matched by positionfunc(name=value) Caller Keyword argument: matched by namefunc(*name) Caller Pass all objects in name as individual positional argumentsfunc(**name) Caller Pass all key/value pairs in name as individual keyword argumentsdef func(name) Function Normal argument: matches any by position or namedef func(name=value) Function Default argument value, if not passed in the calldef func(*name) Function Matches and collects remaining positional arguments (in a tuple)def func(**name) Function Matches and collects remaining keyword arguments (in a dictionary)In a call (the first four rows of the table), simple names are matched by position, butusing the name=value form tells <strong>Python</strong> to match by name instead; these are calledkeyword arguments. Using a * or ** in a call allows us to package up arbitrarily manypositional or keyword objects in sequences and dictionaries, respectively.In a function header, a simple name is matched by position or name (depending onhow the caller passes it), but the name=value form specifies a default value. The *nameform collects any extra unmatched positional arguments in a tuple, and the **nameform collects extra keyword arguments in a dictionary.Of these, keyword arguments and defaults are probably the most commonly used in<strong>Python</strong> code. Keywords allow us to label arguments with their names to make callsmore meaningful. We met defaults earlier, as a way to pass in values from the enclosingfunction’s scope, but they actually are more general than that—they allow us to makeany argument optional, and provide its default value in a function definition.Special Argument-Matching Modes | 331

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

Saved successfully!

Ooh no, something went wrong!