functools.partial could not only applied to functions it also works with classes. This opens some interesting perspectives, like on-the-fly creation of class adapters, as the following code illustrates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | from collections import namedtuple
Point = namedtuple('Point', 'x, y')
POINTS = (
Point(610, 102),
Point(253, 241),
Point(341, 446),
Point(357, 42),
Point(153 ,336),
Point(203, 125),
Point(306, 492),
Point(335, 57),
Point(38, 400),
Point(191, 357),
# and many, many more points.
)
print POINTS
##
## Ok, we decided 3D is way cooler.
## But only a few points have a z coordinate != 0.
##
Point = namedtuple('Point', 'x, y z')
##
## The solution is quite simple.
## Our on-the-fly partial adapter!
##
from functools import partial
Point = partial(Point, z = 0)
POINTS = (
Point(610, 102),
Point(253, 241),
Point(341, 446, z = 23),
# But, however, our defaults have to be specified as keyword arguments.
# Otherwise we got a complaint:
# TypeError: __new__() got multiple values for keyword argument 'z'
Point(357, 42),
Point(153 ,336, z = 11),
Point(203, 125),
Point(306, 492, z = 42),
Point(335, 57),
Point(38, 400, z = 47),
Point(191, 357),
)
print POINTS
|