15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

additional non-keyword arg: 30<br />

additional non-keyword arg: 40<br />

additional keyword arg 'foo': 50<br />

additional keyword arg 'bar': 60<br />

We will now make a similar call; however, instead of listing the variable arguments individually, we will<br />

put the non-keyword arguments in a tuple and the keyword arguments in a dictionary to make the call:<br />

>>> newfoo(2, 4, *(6, 8), **{'foo': 10, 'bar': 12})<br />

arg1 is: 2<br />

arg2 is: 4<br />

additional non-keyword arg: 6<br />

additional non-keyword arg: 8<br />

additional keyword arg 'foo': 10<br />

additional keyword arg 'bar': 12<br />

Finally, we will make another call but build our tuple and dictionary outside of the function invocation:<br />

>>> aTuple = (6, 7, 8)<br />

>>> aDict = {'z': 9}<br />

>>> newfoo(1, 2, 3, x=4, y=5, *aTuple, **aDict)<br />

arg1 is: 1<br />

arg2 is: 2<br />

additional non-keyword arg: 3<br />

additional non-keyword arg: 6<br />

additional non-keyword arg: 7<br />

additional non-keyword arg: 8<br />

additional keyword arg 'z': 9<br />

additional keyword arg 'x': 4<br />

additional keyword arg 'y': 5<br />

Notice how our tuple and dictionary arguments make only a subset of the final tuple and dictionary<br />

received within the function call. The additional non-keyword value '3' and keyword pairs for 'x' and<br />

'y' were also included in the final argument lists even though they were not part of the '*' and '**'<br />

variable argument parameters.<br />

Prior to 1.6, variable objects could only be passed to apply() with the function object for invocation. This<br />

current calling syntax effectively obsoletes the use of apply(). Below is an example of using these<br />

symbols to call any function object with any type of parameter set.<br />

Functional <strong>Programming</strong> Example<br />

Another useful application of functional programming comes in terms of debugging or performance<br />

measurement. You are working on functions that need to be fully tested or run through regressions<br />

every night, or that need to be timed over many iterations for potential improvements. All you need to<br />

do is to create a diagnostic function that sets up the test environment, then calls the function in<br />

question. Because this system should be flexible, you want to allow the testee function to be passed in<br />

as an argument. So a pair of such functions, timeit() and testit(), would probably be useful to the<br />

software developer today.<br />

We will now present the source code to one such example of a testit() function (see Example 11.5).

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

Saved successfully!

Ooh no, something went wrong!