summaryrefslogtreecommitdiffstats
path: root/Lib/collections.py
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-07 17:19:16 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-07 17:19:16 (GMT)
commit043d6f67c7b79a6d268c6ad31d8ff7710ac3e5ee (patch)
tree0ea113cd3a06b4ecbb27a82154174e1846ce1804 /Lib/collections.py
parent13a7a21258f0cd241c2cf1367a954d6742daa2a6 (diff)
downloadcpython-043d6f67c7b79a6d268c6ad31d8ff7710ac3e5ee.zip
cpython-043d6f67c7b79a6d268c6ad31d8ff7710ac3e5ee.tar.gz
cpython-043d6f67c7b79a6d268c6ad31d8ff7710ac3e5ee.tar.bz2
Copied doc for reload() from trunk's function.rst to imp.rst
Diffstat (limited to 'Lib/collections.py')
-rw-r--r--Lib/collections.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 504ae19..884c91f 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -65,9 +65,9 @@ def namedtuple(typename, field_names, verbose=False):
def __new__(cls, %(argtxt)s):
return tuple.__new__(cls, (%(argtxt)s)) \n
@classmethod
- def _make(cls, iterable):
+ def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new %(typename)s object from a sequence or iterable'
- result = tuple.__new__(cls, iterable)
+ result = new(cls, iterable)
if len(result) != %(numfields)d:
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
return result \n
@@ -115,8 +115,22 @@ if __name__ == '__main__':
assert p == loads(dumps(p))
# test and demonstrate ability to override methods
- Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self
- print(p)
+ class Point(namedtuple('Point', 'x y')):
+ @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)
+
+ print(Point(3, 4),'\n', Point(2, 5), '\n', Point(9./7, 6))
+
+ class Point(namedtuple('Point', 'x y')):
+ 'Point class with optimized _make() and _replace() without error-checking'
+ _make = classmethod(tuple.__new__)
+ def _replace(self, _map=map, **kwds):
+ return self._make(_map(kwds.pop, ('x', 'y'), self))
+
+ print(Point(11, 22)._replace(x=100))
import doctest
TestResults = namedtuple('TestResults', 'failed attempted')