Functional Python Programming

(Wang) #1

Conditional Expressions and the Operator Module


[YearCheese(year=2000, cheese=29.87), YearCheese(year=2001,
cheese=30.12), YearCheese(year=2002, cheese=30.6),
YearCheese(year=2003, cheese=30.66), YearCheese(year=2004,
cheese=31.33), YearCheese(year=2005, cheese=32.62),
YearCheese(year=2006, cheese=32.73), YearCheese(year=2007,
cheese=33.5), YearCheese(year=2008, cheese=32.84),
YearCheese(year=2009, cheese=33.02), YearCheese(year=2010,
cheese=32.92)]


We can use lambda forms or we can use the attrgetter() function, as follows:





min(year_cheese_2, key=attrgetter('cheese'))





YearCheese(year=2000, cheese=29.87)





max(year_cheese_2, key=lambda x: x.cheese)





YearCheese(year=2007, cheese=33.5)


What's important here is that with a lambda object, the attribute name is expressed as
a token in the code. With the attrgetter() function, the attribute name is a character
string. This could be a parameter, which allows us to be considerably flexible.


Starmapping with operators


The itertools.starmap() function can be applied to an operator and a sequence
of pairs of values. Here's an example:





d= starmap(pow, zip_longest([], range(4), fillvalue=60))





The itertools.zip_longest() function will create a sequence of pairs such as
the following:


[(60, 0), (60, 1), (60, 2), (60, 3)]


It does this because we provided two sequences: the [] brackets and the range(4)
parameter. The fillvalue parameter will be used when the shorter sequence runs
out of data.


When we use the starmap() function, each pair becomes the argument to the given
function. In this case, we provided the operator.pow() function, which is the
operator. We've calculated values for [60
0, 601, 602, 60**3]. The value
of the d variable is [1, 60, 3600, 216000].


The starmap() function is useful when we have a sequence of tuples. We have a tidy
equivalence between the map(f, x, y) and starmap(f, zip(x,y)) functions.

Free download pdf