@@ -905,15 +905,15 @@ functionality with a subclass. Here is how to add a calculated field and
905905a fixed-width print format:
906906
907907 >>> class Point (namedtuple (' Point' , ' x y' )):
908- __slots__ = ()
909- @property
910- def hypot(self):
911- return (self.x ** 2 + self.y ** 2) ** 0.5
912- def __str__(self):
913- return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
908+ __slots__ = ()
909+ @property
910+ def hypot(self):
911+ return (self.x ** 2 + self.y ** 2) ** 0.5
912+ def __str__(self):
913+ return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
914914
915915 >>> for p in Point(3 , 4 ), Point(14 , 5 / 7 ):
916- print(p)
916+ print(p)
917917 Point: x= 3.000 y= 4.000 hypot= 5.000
918918 Point: x=14.000 y= 0.714 hypot=14.018
919919
@@ -929,10 +929,10 @@ Docstrings can be customized by making direct assignments to the ``__doc__``
929929fields:
930930
931931 >>> Book = namedtuple(' Book' , [' id' , ' title' , ' authors' ])
932- >>> Book.__doc__ = ' Hardcover book in active collection'
932+ >>> Book.__doc__ + = ' : Hardcover book in active collection'
933933 >>> Book.id.__doc__ = ' 13-digit ISBN'
934934 >>> Book.title.__doc__ = ' Title of first printing'
935- >>> Book.author .__doc__ = ' List of authors sorted by last name'
935+ >>> Book.authors .__doc__ = ' List of authors sorted by last name'
936936
937937Default values can be implemented by using :meth: `_replace ` to
938938customize a prototype instance:
@@ -942,16 +942,6 @@ customize a prototype instance:
942942 >>> johns_account = default_account._replace(owner = ' John' )
943943 >>> janes_account = default_account._replace(owner = ' Jane' )
944944
945- Enumerated constants can be implemented with named tuples, but it is simpler
946- and more efficient to use a simple :class: `~enum.Enum `:
947-
948- >>> Status = namedtuple(' Status' , ' open pending closed' )._make(range (3 ))
949- >>> Status.open, Status.pending, Status.closed
950- (0, 1, 2)
951- >>> from enum import Enum
952- >>> class Status (Enum ):
953- ... open , pending, closed = range (3 )
954-
955945
956946.. seealso ::
957947
0 commit comments