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.

The min Wakeup CallTo make this more concrete, let’s work through an exercise that demonstrates apractical application of argument-matching tools. Suppose you want to code a functionthat is able to compute the minimum value from an arbitrary set of argumentsand an arbitrary set of object data types. That is, the function should accept zero ormore arguments—as many as you wish to pass. Moreover, the function should workfor all kinds of <strong>Python</strong> object types: numbers, strings, lists, lists of dictionaries, files,and even None.The first requirement provides a natural example of how the * feature can be put togood use—we can collect arguments into a tuple, and step over each in turn with asimple for loop. The second part of the problem definition is easy: because everyobject type supports comparisons, we don’t have to specialize the function per type(an application of polymorphism); we can simply compare objects blindly, and let<strong>Python</strong> perform the correct sort of comparison.Full creditThe following file shows three ways to code this operation, at least one of which wassuggested by a student at some point along the way:• The first function fetches the first argument (args is a tuple), and traverses therest by slicing off the first (there’s no point in comparing an object to itself, especiallyif it might be a large structure).• The second version lets <strong>Python</strong> pick off the first and rest of the arguments automatically,and so avoids an index and a slice.• The third converts from a tuple to a list with the built-in list call, and employsthe list sort method.The sort method is coded in C, so it can be quicker than the others at times, but thelinear scans of the first two techniques will make them faster most of the time. * Thefile mins.py contains the code for all three solutions:def min1(*args):res = args[0]for arg in args[1:]:* Actually, this is fairly complicated. The <strong>Python</strong> sort routine is coded in C, and uses a highly optimized algorithmthat attempts to take advantage of partial ordering in the items to be sorted. It’s named “timsort” afterTim Peters, its creator, and in its documentation it claims to have “supernatural performance” at times(pretty good, for a sort!). Still, sorting is an inherently exponential operation (it must chop up the sequence,and put it back together many times), and the other versions simply perform one linear, left to right scan.The net effect is that sorting is quicker if the arguments are partially ordered, but likely slower otherwise.Even so, <strong>Python</strong> performance can change over time, and the fact that sorting is implemented in the Clanguage can help greatly; for an exact analysis, you should time the alternatives with the time or timeitmodules we’ll meet in the next chapter.336 | Chapter 16: Scopes and Arguments

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

Saved successfully!

Ooh no, something went wrong!