@@ -114,7 +114,7 @@ The class can be used to simulate nested scopes and is useful in templating.
114
114
for templating is a read-only chain of mappings. It also features
115
115
pushing and popping of contexts similar to the
116
116
:meth: `~collections.ChainMap.new_child ` method and the
117
- :meth : `~collections.ChainMap.parents ` property.
117
+ :attr : `~collections.ChainMap.parents ` property.
118
118
119
119
* The `Nested Contexts recipe
120
120
<https://code.activestate.com/recipes/577434/> `_ has options to control
@@ -270,7 +270,7 @@ For example::
270
270
271
271
Return a list of the *n * most common elements and their counts from the
272
272
most common to the least. If *n * is omitted or ``None ``,
273
- :func : `most_common ` returns *all * elements in the counter.
273
+ :meth : `most_common ` returns *all * elements in the counter.
274
274
Elements with equal counts are ordered arbitrarily:
275
275
276
276
>>> Counter(' abracadabra' ).most_common(3 ) # doctest: +SKIP
@@ -357,20 +357,20 @@ or subtracting from an empty counter.
357
357
restrictions on its keys and values. The values are intended to be numbers
358
358
representing counts, but you *could * store anything in the value field.
359
359
360
- * The :meth: `most_common ` method requires only that the values be orderable.
360
+ * The :meth: `~Counter. most_common ` method requires only that the values be orderable.
361
361
362
362
* For in-place operations such as ``c[key] += 1 ``, the value type need only
363
363
support addition and subtraction. So fractions, floats, and decimals would
364
364
work and negative values are supported. The same is also true for
365
- :meth: `update ` and :meth: `subtract ` which allow negative and zero values
365
+ :meth: `~Counter. update ` and :meth: `~Counter. subtract ` which allow negative and zero values
366
366
for both inputs and outputs.
367
367
368
368
* The multiset methods are designed only for use cases with positive values.
369
369
The inputs may be negative or zero, but only outputs with positive values
370
370
are created. There are no type restrictions, but the value type needs to
371
371
<
10000
td data-grid-cell-id="diff-5251b40283a63a061147a0ae32017c629d88a95d1c363d4e47f37b7969a4b0e3-371-371-2" data-line-anchor="diff-5251b40283a63a061147a0ae32017c629d88a95d1c363d4e47f37b7969a4b0e3R371" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side"> support addition, subtraction, and comparison.
372
372
373
- * The :meth: `elements ` method requires integer counts. It ignores zero and
373
+ * The :meth: `~Counter. elements ` method requires integer counts. It ignores zero and
374
374
negative counts.
375
375
376
376
.. seealso ::
@@ -388,9 +388,9 @@ or subtracting from an empty counter.
388
388
Section 4.6.3, Exercise 19 *.
389
389
390
390
* To enumerate all distinct multisets of a given size over a given set of
391
- elements, see :func: `itertools.combinations_with_replacement `:
391
+ elements, see :func: `itertools.combinations_with_replacement `::
392
392
393
- map(Counter, combinations_with_replacement('ABC', 2)) --> AA AB AC BB BC CC
393
+ map(Counter, combinations_with_replacement('ABC', 2)) # --> AA AB AC BB BC CC
394
394
395
395
396
396
:class: `deque ` objects
@@ -640,18 +640,18 @@ the :meth:`~deque.rotate` method::
640
640
# Remove an exhausted iterator.
641
641
iterators.popleft()
642
642
643
- The :meth: `rotate ` method provides a way to implement :class: `deque ` slicing and
643
+ The :meth: `~deque. rotate ` method provides a way to implement :class: `deque ` slicing and
644
644
deletion. For example, a pure Python implementation of ``del d[n] `` relies on
645
- the :meth: ` rotate ` method to position elements to be popped::
645
+ the `` rotate() ` ` method to position elements to be popped::
646
646
647
647
def delete_nth(d, n):
648
648
d.rotate(-n)
649
649
d.popleft()
650
650
d.rotate(n)
651
651
652
652
To implement :class: `deque ` slicing, use a similar approach applying
653
- :meth: `rotate ` to bring a target element to the left side of the deque. Remove
654
- old entries with :meth: `popleft `, add new entries with :meth: `extend `, and then
653
+ :meth: `~deque. rotate ` to bring a target element to the left side of the deque. Remove
654
+ old entries with :meth: `~deque. popleft `, add new entries with :meth: `~deque. extend `, and then
655
655
reverse the rotation.
656
656
With minor variations on that approach, it is easy to implement Forth style
657
657
stack manipulations such as ``dup ``, ``drop ``, ``swap ``, ``over ``, ``pick ``,
@@ -712,7 +712,7 @@ stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
712
712
:class: `defaultdict ` Examples
713
713
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
714
714
715
- Using :class: `list ` as the :attr: `default_factory `, it is easy to group a
715
+ Using :class: `list ` as the :attr: `~defaultdict. default_factory `, it is easy to group a
716
716
sequence of key-value pairs into a dictionary of lists:
717
717
718
718
>>> s = [(' yellow' , 1 ), (' blue' , 2 ), (' yellow' , 3 ), (' blue' , 4 ), (' red' , 1 )]
@@ -724,7 +724,7 @@ sequence of key-value pairs into a dictionary of lists:
724
724
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
725
725
726
726
When each key is encountered for the first time, it is not already in the
727
- mapping; so an entry is automatically created using the :attr: `default_factory `
727
+ mapping; so an entry is automatically created using the :attr: `~defaultdict. default_factory `
728
728
function which returns an empty :class: `list `. The :meth: `list.append `
729
729
operation then attaches the value to the new list. When keys are encountered
730
730
again, the look-up proceeds normally (returning the list for that key) and the
@@ -738,7 +738,7 @@ simpler and faster than an equivalent technique using :meth:`dict.setdefault`:
738
738
>>> sorted (d.items())
739
739
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
740
740
741
- Setting the :attr: `default_factory ` to :class: `int ` makes the
741
+ Setting the :attr: `~defaultdict. default_factory ` to :class: `int ` makes the
742
742
:class: `defaultdict ` useful for counting (like a bag or multiset in other
743
743
languages):
744
744
@@ -751,7 +751,7 @@ languages):
751
751
[('i', 4), ('m', 1), ('p', 2), ('s', 4)]
752
752
753
753
When a letter is first encountered, it is missing from the mapping, so the
754
- :attr: `default_factory ` function calls :func: `int ` to supply a default count of
754
+ :attr: `~defaultdict. default_factory ` function calls :func: `int ` to supply a default count of
755
755
zero. The increment operation then builds up the count for each letter.
756
756
757
757
The function :func: `int ` which always returns zero is just a special case of
@@ -766,7 +766,7 @@ zero):
766
766
>>> ' %(name)s %(action)s to %(object)s ' % d
767
767
'John ran to <missing>'
768
768
769
- Setting the :attr: `default_factory ` to :class: `set ` makes the
769
+ Setting the :attr: `~defaultdict. default_factory ` to :class: `set ` makes the
770
770
:class: `defaultdict ` useful for building a dictionary of sets:
771
771
772
772
>>> s = [(' red' , 1 ), (' blue' , 2 ), (' red' , 3 ), (' blue' , 4 ), (' red' , 1 ), (' blue' , 4 )]
@@ -973,7 +973,7 @@ The subclass shown above sets ``__slots__`` to an empty tuple. This helps
973
973
keep memory requirements low by preventing the creation of instance dictionaries.
974
974
975
975
Subclassing is not useful for adding new, stored fields. Instead, simply
976
- create a new named tuple type from the :attr: `_fields ` attribute:
976
+ create a new named tuple type from the :attr: `~somenamedtuple. _fields ` attribute:
977
977
978
978
>>> Point3D = namedtuple(' Point3D' , Point._fields + (' z' ,))
979
979
@@ -989,7 +989,7 @@ fields:
989
989
.. versionchanged :: 3.5
990
990
Property docstrings became writeable.
991
991
992
- Default values can be implemented by using :meth: `_replace ` to
992
+ Default values can be implemented by using :meth: `~somenamedtuple. _replace ` to
993
993
customize a prototype instance:
994
994
995
995
>>> Account = namedtuple(' Account' , ' owner balance transaction_count' )
@@ -1200,15 +1200,22 @@ subclass directly from :class:`str`; however, this class can be easier
1200
1200
to work with because the underlying string is accessible as an
1201
1201
attribute.
1202
1202
1203
- .. class :: UserString([sequence] )
1203
+ .. class :: UserString(seq )
1204
1204
1205
- Class that simulates a string or a Unicode string object. The instance's
1205
+ Class that simulates a string object. The instance's
1206
1206
content is kept in a regular string object, which is accessible via the
1207
1207
:attr: `data ` attribute of :class: `UserString ` instances. The instance's
1208
- contents are initially set to a copy of *sequence *. The *sequence * can
1209
- be an instance of :class: `bytes `, :class: `str `, :class: `UserString ` (or a
1210
- subclass) or an arbitrary sequence which can be converted into a string using
1211
- the built-in :func: `str ` function.
1208
+ contents are initially set to a copy of *seq *. The *seq * argument can
1209
+ be any object which can be converted into a string using the built-in
1210
+ :func: `str ` function.
1211
+
1212
+ In addition to supporting the methods and operations of strings,
1213
+ :class: `UserString ` instances provide the following attribute:
1214
+
1215
+ .. attribute :: data
1216
+
1217
+ A real :class: `str ` object used to store the contents of the
1218
+ :class: `UserString ` class.
1212
1219
1213
1220
.. versionchanged :: 3.5
1214
1221
New methods ``__getnewargs__ ``, ``__rmod__ ``, ``casefold ``,
0 commit comments