Cleanup named tuple subclassing example.

This commit is contained in:
Raymond Hettinger 2008-01-07 20:07:38 +00:00
parent 4d7e6702e9
commit 9a359210aa
2 changed files with 16 additions and 13 deletions

View File

@ -365,7 +365,7 @@ they add the ability to access fields by name instead of position index.
The *fieldnames* are a single string with each fieldname separated by whitespace
and/or commas (for example 'x y' or 'x, y'). Alternatively, the *fieldnames*
can be specified as a list of strings (such as ['x', 'y']).
can be specified with a sequence of strings (such as ['x', 'y']).
Any valid Python identifier may be used for a fieldname except for names
starting with an underscore. Valid identifiers consist of letters, digits,
@ -478,7 +478,7 @@ three additional methods and one attribute.
Point(x=33, y=22)
>>> for partnum, record in inventory.items():
... inventory[partnum] = record._replace(price=newprices[partnum], updated=time.now())
inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())
.. attribute:: somenamedtuple._fields
@ -515,13 +515,15 @@ a fixed-width print format::
@property
def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def __repr__(self):
return 'Point(x=%.3f, y=%.3f, hypot=%.3f)' % (self.x, self.y, self.hypot)
def __str__(self):
return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
>>> print Point(3, 4),'\n', Point(2, 5), '\n', Point(9./7, 6)
Point(x=3.000, y=4.000, hypot=5.000)
Point(x=2.000, y=5.000, hypot=5.385)
Point(x=1.286, y=6.000, hypot=6.136)
>>> for p in Point(3,4), Point(14,5), Point(9./7,6):
print p
Point: x= 3.000 y= 4.000 hypot= 5.000
Point: x=14.000 y= 5.000 hypot=14.866
Point: x= 1.286 y= 6.000 hypot= 6.136
Another use for subclassing is to replace performance critcal methods with
faster versions that bypass error-checking and localize variable access::
@ -531,8 +533,8 @@ faster versions that bypass error-checking and localize variable access::
def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.pop, ('x', 'y'), self))
Default values can be implemented by starting with a prototype instance
and customizing it with :meth:`_replace`::
Default values can be implemented by using :meth:`_replace`:: to
customize a prototype instance::
>>> Account = namedtuple('Account', 'owner balance transaction_count')
>>> model_account = Account('<owner name>', 0.0, 0)

View File

@ -120,10 +120,11 @@ if __name__ == '__main__':
@property
def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def __repr__(self):
return 'Point(x=%.3f, y=%.3f, hypot=%.3f)' % (self.x, self.y, self.hypot)
def __str__(self):
return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
print Point(3, 4),'\n', Point(2, 5), '\n', Point(9./7, 6)
for p in Point(3,4), Point(14,5), Point(9./7,6):
print p
class Point(namedtuple('Point', 'x y')):
'Point class with optimized _make() and _replace() without error-checking'