[Python-checkins] r59840 - in python/trunk: Doc/library/collections.rst Lib/collections.py

raymond.hettinger python-checkins at python.org
Mon Jan 7 21:07:38 CET 2008


Author: raymond.hettinger
Date: Mon Jan  7 21:07:38 2008
New Revision: 59840

Modified:
   python/trunk/Doc/library/collections.rst
   python/trunk/Lib/collections.py
Log:
Cleanup named tuple subclassing example.

Modified: python/trunk/Doc/library/collections.rst
==============================================================================
--- python/trunk/Doc/library/collections.rst	(original)
+++ python/trunk/Doc/library/collections.rst	Mon Jan  7 21:07:38 2008
@@ -365,7 +365,7 @@
 
    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 @@
       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 @@
         @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 @@
         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)

Modified: python/trunk/Lib/collections.py
==============================================================================
--- python/trunk/Lib/collections.py	(original)
+++ python/trunk/Lib/collections.py	Mon Jan  7 21:07:38 2008
@@ -120,10 +120,11 @@
         @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'


More information about the Python-checkins mailing list