summaryrefslogtreecommitdiffstats
path: root/Doc/library/collections.rst
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-07 21:14:23 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-07 21:14:23 (GMT)
commit790c8232019d0a13c3f0a72b8cffcf3ae69ea7b9 (patch)
tree377ebd7133b8766eee491cefe5b6d5eb5717d145 /Doc/library/collections.rst
parent0625e89771e17e3ed5ca1fb37e0fdc9224fc5a2a (diff)
downloadcpython-790c8232019d0a13c3f0a72b8cffcf3ae69ea7b9.zip
cpython-790c8232019d0a13c3f0a72b8cffcf3ae69ea7b9.tar.gz
cpython-790c8232019d0a13c3f0a72b8cffcf3ae69ea7b9.tar.bz2
Merged revisions 59822-59841 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r59822 | georg.brandl | 2008-01-07 17:43:47 +0100 (Mon, 07 Jan 2008) | 2 lines Restore "somenamedtuple" as the "class" for named tuple attrs. ........ r59824 | georg.brandl | 2008-01-07 18:09:35 +0100 (Mon, 07 Jan 2008) | 2 lines Patch #602345 by Neal Norwitz and me: add -B option and PYTHONDONTWRITEBYTECODE envvar to skip writing bytecode. ........ r59827 | georg.brandl | 2008-01-07 18:25:53 +0100 (Mon, 07 Jan 2008) | 2 lines patch #1668: clarify envvar docs; rename THREADDEBUG to PYTHONTHREADDEBUG. ........ r59830 | georg.brandl | 2008-01-07 19:16:36 +0100 (Mon, 07 Jan 2008) | 2 lines Make Python compile with --disable-unicode. ........ r59831 | georg.brandl | 2008-01-07 19:23:27 +0100 (Mon, 07 Jan 2008) | 2 lines Restructure urllib doc structure. ........ r59833 | georg.brandl | 2008-01-07 19:41:34 +0100 (Mon, 07 Jan 2008) | 2 lines Fix #define ordering. ........ r59834 | georg.brandl | 2008-01-07 19:47:44 +0100 (Mon, 07 Jan 2008) | 2 lines #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall. ........ r59835 | raymond.hettinger | 2008-01-07 19:52:19 +0100 (Mon, 07 Jan 2008) | 1 line Fix inconsistent title levels -- it made the whole doc build crash horribly. ........ r59836 | georg.brandl | 2008-01-07 19:57:03 +0100 (Mon, 07 Jan 2008) | 2 lines Fix two further doc build warnings. ........ r59837 | georg.brandl | 2008-01-07 20:17:10 +0100 (Mon, 07 Jan 2008) | 2 lines Clarify metaclass docs and add example. ........ r59838 | vinay.sajip | 2008-01-07 20:40:10 +0100 (Mon, 07 Jan 2008) | 1 line Added section about adding contextual information to log output. ........ r59839 | christian.heimes | 2008-01-07 20:58:41 +0100 (Mon, 07 Jan 2008) | 1 line Fixed indention problem that caused the second TIPC test to run on systems without TIPC ........ r59840 | raymond.hettinger | 2008-01-07 21:07:38 +0100 (Mon, 07 Jan 2008) | 1 line Cleanup named tuple subclassing example. ........
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r--Doc/library/collections.rst38
1 files changed, 18 insertions, 20 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index cb3a029..2b8e279 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -382,7 +382,7 @@ Setting the :attr:`default_factory` to :class:`set` makes the
.. _named-tuple-factory:
:func:`namedtuple` Factory Function for Tuples with Named Fields
------------------------------------------------------------------
+----------------------------------------------------------------
Named tuples assign meaning to each position in a tuple and allow for more readable,
self-documenting code. They can be used wherever regular tuples are used, and
@@ -398,7 +398,7 @@ they add the ability to access fields by name instead of position index.
The *fieldnames* are a single string with each fieldname separated by whitespace
and/or commas (for example 'x y' or 'x, y'). Alternatively, the *fieldnames*
- can be specified as a list of strings (such as ['x', 'y']).
+ can be specified with a sequence of strings (such as ['x', 'y']).
Any valid Python identifier may be used for a fieldname except for names
starting with an underscore. Valid identifiers consist of letters, digits,
@@ -479,7 +479,7 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
In addition to the methods inherited from tuples, named tuples support
three additional methods and one attribute.
-.. method:: namedtuple._make(iterable)
+.. method:: somenamedtuple._make(iterable)
Class method that makes a new instance from an existing sequence or iterable.
@@ -489,7 +489,7 @@ three additional methods and one attribute.
>>> Point._make(t)
Point(x=11, y=22)
-.. method:: namedtuple._asdict()
+.. method:: somenamedtuple._asdict()
Return a new dict which maps field names to their corresponding values:
@@ -498,7 +498,7 @@ three additional methods and one attribute.
>>> p._asdict()
{'x': 11, 'y': 22}
-.. method:: namedtuple._replace(kwargs)
+.. method:: somenamedtuple._replace(kwargs)
Return a new instance of the named tuple replacing specified fields with new values:
@@ -509,9 +509,9 @@ three additional methods and one attribute.
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], timestamp=time.now())
-.. attribute:: namedtuple._fields
+.. attribute:: somenamedtuple._fields
Tuple of strings listing the field names. This is useful for introspection
and for creating new named tuple types from existing named tuples.
@@ -527,9 +527,7 @@ three additional methods and one attribute.
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:
-
-::
+function::
>>> getattr(p, 'x')
11
@@ -548,13 +546,15 @@ a fixed-width print format::
@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)
+ def __str__(self):
+ return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
+
+ >>> for p in Point(3,4), Point(14,5), Point(9./7,6):
+ print p
- >>> print Point(3, 4),'\n', Point(2, 5), '\n', Point(9./7, 6)
- Point(x=3.000, y=4.000, hypot=5.000)
- Point(x=2.000, y=5.000, hypot=5.385)
- Point(x=1.286, y=6.000, hypot=6.136)
+ Point: x= 3.000 y= 4.000 hypot= 5.000
+ Point: x=14.000 y= 5.000 hypot=14.866
+ Point: x= 1.286 y= 6.000 hypot= 6.136
Another use for subclassing is to replace performance critcal methods with
faster versions that bypass error-checking and localize variable access::
@@ -564,10 +564,8 @@ faster versions that bypass error-checking and localize variable access::
def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.pop, ('x', 'y'), self))
-Default values can be implemented by starting with a prototype instance
-and customizing it with :meth:`_replace`:
-
-::
+Default values can be implemented by using :meth:`_replace`:: to
+customize a prototype instance::
>>> Account = namedtuple('Account', 'owner balance transaction_count')
>>> model_account = Account('<owner name>', 0.0, 0)