################################################################################### Simple tests################################################################################# verify that instances can be pickledfromcollectionsimportnamedtuplefrompickleimportloads,dumpsPoint=namedtuple('Point','x, y',True)p=Point(x=10,y=20)assertp==loads(dumps(p))# test and demonstrate ability to override methodsclassPoint(namedtuple('Point','x y')):__slots__=()@propertydefhypot(self):return(self.x**2+self.y**2)**0.5def__str__(self):return'Point: x=%6.3f y=%6.3f hypot=%6.3f'%(self.x,self.y,self.hypot)forpinPoint(3,4),Point(14,5/7.):print(p)classPoint(namedtuple('Point','x y')):'Point class with optimized _make() and _replace() without error-checking'__slots__=()_make=classmethod(tuple.__new__)def_replace(self,_map=map,**kwds):returnself._make(_map(kwds.get,('x','y'),self))print(Point(11,22)._replace(x=100))Point3D=namedtuple('Point3D',Point._fields+('z',))print(Point3D.__doc__)importdoctest,collectionsTestResults=namedtuple('TestResults','failed attempted')print(TestResults(*doctest.testmod(collections)))