diff options
author | Raymond Hettinger <python@rcn.com> | 2007-12-18 00:13:45 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-12-18 00:13:45 (GMT) |
commit | 88880b2dd6677575586b8c0f5aaffd18f260afa7 (patch) | |
tree | c0126ceb66fad49a1652ac170f3676192b2be00d /Lib/collections.py | |
parent | e64acfad3dd13809943eecf50f11c40c510dedbe (diff) | |
download | cpython-88880b2dd6677575586b8c0f5aaffd18f260afa7.zip cpython-88880b2dd6677575586b8c0f5aaffd18f260afa7.tar.gz cpython-88880b2dd6677575586b8c0f5aaffd18f260afa7.tar.bz2 |
Add more namedtuple() test cases. Neaten the code and comments.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index be4d4fd..a43fdfa 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -1,4 +1,9 @@ __all__ = ['deque', 'defaultdict', 'namedtuple'] +# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. +# They should however be considered an integral part of collections.py. +from _abcoll import * +import _abcoll +__all__ += _abcoll.__all__ from _collections import deque, defaultdict from operator import itemgetter as _itemgetter @@ -6,12 +11,6 @@ from itertools import izip as _izip from keyword import iskeyword as _iskeyword import sys as _sys -# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. -# They should however be considered an integral part of collections.py. -from _abcoll import * -import _abcoll -__all__ += _abcoll.__all__ - def namedtuple(typename, field_names, verbose=False): """Returns a new subclass of tuple with named fields. @@ -19,9 +18,9 @@ def namedtuple(typename, field_names, verbose=False): >>> Point.__doc__ # docstring for the new class 'Point(x, y)' >>> p = Point(11, y=22) # instantiate with positional args or keywords - >>> p[0] + p[1] # works just like the tuple (11, 22) + >>> p[0] + p[1] # indexable like a plain tuple: (11, 22) 33 - >>> x, y = p # unpacks just like a tuple + >>> x, y = p # unpack like a regular tuple >>> x, y (11, 22) >>> p.x + p.y # fields also accessable by name @@ -61,7 +60,6 @@ def namedtuple(typename, field_names, verbose=False): template = '''class %(typename)s(tuple): '%(typename)s(%(argtxt)s)' \n __slots__ = () \n - _fields = property(lambda self: %(field_names)r) \n def __new__(cls, %(argtxt)s): return tuple.__new__(cls, (%(argtxt)s)) \n def __repr__(self): @@ -71,7 +69,10 @@ def namedtuple(typename, field_names, verbose=False): return dict(zip(%(field_names)r, self)) \n def _replace(self, **kwds): 'Return a new %(typename)s object replacing specified fields with new values' - return %(typename)s(*map(kwds.get, %(field_names)r, self)) \n\n''' % locals() + return %(typename)s(*map(kwds.get, %(field_names)r, self)) \n + @property + def _fields(self): + return %(field_names)r \n\n''' % locals() for i, name in enumerate(field_names): template += ' %s = property(itemgetter(%d))\n' % (name, i) if verbose: |