Merged revisions 59488-59511 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line

  Silence a warning about an unsed variable in debug builds
........
  r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines

  Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
  I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
  r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line

  Cleaner method naming convention
........
  r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line

  Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
  r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line

  Fixed warning in ssl module
........
  r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line

  Add line spacing for readability
........
  r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines

  Update method names for named tuples.
........
  r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines

  Add a section about nested listcomps to the tutorial.
  Thanks to Ian Bruntlett and Robert Lehmann.
........
  r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line

  Faster and simpler _replace() method
........
  r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line

  Add usage note
........
  r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line

  Remove warning about URL
........
  r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line

  Bump the version number, and make a few small edits
........
  r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines

  Fixed bug #1628
  The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
This commit is contained in:
Christian Heimes 2007-12-15 01:27:15 +00:00
parent 54cc54c1fe
commit 0449f63f53
16 changed files with 299 additions and 103 deletions

View file

@ -403,8 +403,8 @@ they add the ability to access fields by name instead of position index.
can be specified as a list of strings (such as ['x', 'y']).
Any valid Python identifier may be used for a fieldname except for names
starting and ending with double underscores. Valid identifiers consist of
letters, digits, and underscores but do not start with a digit and cannot be
starting with an underscore. Valid identifiers consist of letters, digits,
and underscores but do not start with a digit or underscore and cannot be
a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*, *print*,
or *raise*.
@ -418,18 +418,25 @@ Example::
>>> Point = namedtuple('Point', 'x y', verbose=True)
class Point(tuple):
'Point(x, y)'
__slots__ = ()
__fields__ = ('x', 'y')
_fields = ('x', 'y')
def __new__(cls, x, y):
return tuple.__new__(cls, (x, y))
def __repr__(self):
return 'Point(x=%r, y=%r)' % self
def __asdict__(self):
'Return a new dict mapping field names to their values'
def _asdict(self):
'Return a new dict which maps field names to their values'
return dict(zip(('x', 'y'), self))
def __replace__(self, **kwds):
def _replace(self, **kwds):
'Return a new Point object replacing specified fields with new values'
return Point(**dict(zip(('x', 'y'), self), **kwds))
return Point(*map(kwds.get, ('x', 'y'), self))
x = property(itemgetter(0))
y = property(itemgetter(1))
@ -477,43 +484,51 @@ When casting a dictionary to a named tuple, use the double-star-operator::
In addition to the methods inherited from tuples, named tuples support
two additonal methods and a read-only attribute.
.. method:: somenamedtuple.__asdict__()
.. method:: somenamedtuple._asdict()
Return a new dict which maps field names to their corresponding values:
::
>>> p.__asdict__()
>>> p._asdict()
{'x': 11, 'y': 22}
.. method:: somenamedtuple.__replace__(kwargs)
.. method:: somenamedtuple._replace(kwargs)
Return a new instance of the named tuple replacing specified fields with new values:
::
>>> p = Point(x=11, y=22)
>>> p.__replace__(x=33)
>>> p._replace(x=33)
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], updated=time.now())
.. attribute:: somenamedtuple.__fields__
.. attribute:: somenamedtuple._fields
Return a tuple of strings listing the field names. This is useful for introspection
and for creating new named tuple types from existing named tuples.
::
>>> p.__fields__ # view the field names
>>> p._fields # view the field names
('x', 'y')
>>> Color = namedtuple('Color', 'red green blue')
>>> Pixel = namedtuple('Pixel', Point.__fields__ + Color.__fields__)
>>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
>>> Pixel(11, 22, 128, 255, 0)
Pixel(x=11, y=22, red=128, green=255, blue=0)'
To retrieve a field whose name is stored in a string, use the :func:`getattr`
function:
::
>>> getattr(p, 'x')
11
Since a named tuple is a regular Python class, it is easy to add or change
functionality. For example, the display format can be changed by overriding
the :meth:`__repr__` method:
@ -522,17 +537,17 @@ the :meth:`__repr__` method:
>>> Point = namedtuple('Point', 'x y')
>>> Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self
>>> Point(x=10, y=20)
Point(10.000, 20.000)
>>> Point(x=11, y=22)
Point(11.000, 22.000)
Default values can be implemented by starting with a prototype instance
and customizing it with :meth:`__replace__`:
and customizing it with :meth:`_replace`:
::
>>> Account = namedtuple('Account', 'owner balance transaction_count')
>>> model_account = Account('<owner name>', 0.0, 0)
>>> johns_account = model_account.__replace__(owner='John')
>>> johns_account = model_account._replace(owner='John')
.. rubric:: Footnotes