Examples for named tuple subclassing should include __slots__

This commit is contained in:
Raymond Hettinger 2008-01-10 19:15:10 +00:00
parent 44c38c16b8
commit e1655088ca
2 changed files with 7 additions and 4 deletions

View File

@ -513,23 +513,24 @@ functionality with a subclass. Here is how to add a calculated field and
a fixed-width print format:: a fixed-width print format::
>>> class Point(namedtuple('Point', 'x y')): >>> class Point(namedtuple('Point', 'x y')):
... __slots__ = ()
... @property ... @property
... def hypot(self): ... def hypot(self):
... return (self.x ** 2 + self.y ** 2) ** 0.5 ... return (self.x ** 2 + self.y ** 2) ** 0.5
... def __str__(self): ... def __str__(self):
... return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) ... return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
>>> for p in Point(3,4), Point(14,5), Point(9./7,6): >>> for p in Point(3, 4), Point(14, 5/7.):
... print p ... print p
Point: x= 3.000 y= 4.000 hypot= 5.000 Point: x= 3.000 y= 4.000 hypot= 5.000
Point: x=14.000 y= 5.000 hypot=14.866 Point: x=14.000 y= 0.714 hypot=14.018
Point: x= 1.286 y= 6.000 hypot= 6.136
Another use for subclassing is to replace performance critcal methods with Another use for subclassing is to replace performance critcal methods with
faster versions that bypass error-checking and that localize variable access:: faster versions that bypass error-checking and that localize variable access::
class Point(namedtuple('Point', 'x y')): class Point(namedtuple('Point', 'x y')):
__slots__ = ()
_make = classmethod(tuple.__new__) _make = classmethod(tuple.__new__)
def _replace(self, _map=map, **kwds): def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self)) return self._make(_map(kwds.get, ('x', 'y'), self))

View File

@ -118,17 +118,19 @@ if __name__ == '__main__':
# test and demonstrate ability to override methods # test and demonstrate ability to override methods
class Point(namedtuple('Point', 'x y')): class Point(namedtuple('Point', 'x y')):
__slots__ = ()
@property @property
def hypot(self): def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5 return (self.x ** 2 + self.y ** 2) ** 0.5
def __str__(self): def __str__(self):
return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
for p in Point(3,4), Point(14,5), Point(9./7,6): for p in Point(3, 4), Point(14, 5/7.):
print p print p
class Point(namedtuple('Point', 'x y')): class Point(namedtuple('Point', 'x y')):
'Point class with optimized _make() and _replace() without error-checking' 'Point class with optimized _make() and _replace() without error-checking'
__slots__ = ()
_make = classmethod(tuple.__new__) _make = classmethod(tuple.__new__)
def _replace(self, _map=map, **kwds): def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self)) return self._make(_map(kwds.get, ('x', 'y'), self))