Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed54d91

Browse files
committed
Various fixups:
* Add comment on the future of the sets module. * Change a variable from "input" to "data" to avoid shadowing a builtin. * Added possible applications for str.rsplit() and itertools.tee(). * Repaired the example for sorted(). * Cleaned-up the example for operator.itemgetter().
1 parent 32fef9f commit ed54d91

1 file changed

Lines changed: 36 additions & 26 deletions

File tree

Doc/whatsnew/whatsnew24.tex

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ \section{PEP 218: Built-In Set Objects}
7070
like \method{add()} and \method{remove()} which could alter its contents.
7171

7272
% XXX what happens to the sets module?
73+
% The current thinking is that the sets module will be left alone.
74+
% That way, existing code will continue to run without alteration.
75+
% Also, the module provides an autoconversion feature not supported by set()
76+
% and frozenset().
7377

7478
\begin{seealso}
7579
\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
@@ -105,8 +109,8 @@ \section{PEP 322: Reverse Iteration}
105109
a list with \function{list()}.
106110

107111
\begin{verbatim}
108-
>>> input = open('/etc/passwd', 'r')
109-
>>> for line in reversed(list(input)):
112+
>>> data = open('/etc/passwd', 'r')
113+
>>> for line in reversed(list(data)):
110114
... print line
111115
...
112116
root:*:0:0:System Administrator:/var/root:/bin/tcsh
@@ -132,7 +136,9 @@ \section{Other Language Changes}
132136
fill character other than a space.
133137

134138
\item Strings also gained an \method{rsplit()} method that
135-
works like the \method{split()} method but splits from the end of the string.
139+
works like the \method{split()} method but splits from the end of
140+
the string. Possible applications include splitting a filename
141+
from a path or a domain name from URL.
136142

137143
\begin{verbatim}
138144
>>> 'a b c'.split(None, 1)
@@ -169,7 +175,7 @@ \section{Other Language Changes}
169175
\end{verbatim}
170176

171177
The last example, which uses the \var{cmp} parameter, is the old way
172-
to perform a case-insensitive sort. It works, but is slower than
178+
to perform a case-insensitive sort. It works but is slower than
173179
using a \var{key} parameter. Using \var{key} results in calling the
174180
\method{lower()} method once for each element in the list while using
175181
\var{cmp} will call the method twice for each comparison.
@@ -230,7 +236,7 @@ \section{Other Language Changes}
230236

231237
\item The \function{zip()} built-in function and \function{itertools.izip()}
232238
now return an empty list instead of raising a \exception{TypeError}
233-
exception if called with no arguments. This makes the functions more
239+
exception if called with no arguments. This makes the function more
234240
suitable for use with variable length argument lists:
235241

236242
\begin{verbatim}
@@ -319,36 +325,41 @@ \section{New, Improved, and Deprecated Modules}
319325

320326
\begin{verbatim}
321327
>>> word = 'abracadabra'
322-
>>> letters = sorted(word) # Turn string into sorted list of letters
328+
>>> letters = sorted(word) # Turn string into a sorted list of letters
323329
>>> letters
324330
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
325-
>>> [k for k, g in groupby(word)] # List unique letters
331+
>>> [k for k, g in groupby(letters)] # List unique letters
326332
['a', 'b', 'c', 'd', 'r']
327-
>>> [(k, len(list(g))) for k, g in groupby(word)] # Count letter occurences
333+
>>> [(k, len(list(g))) for k, g in groupby(letters)] # Count letter occurences
328334
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
329-
>>> [k for k, g in groupby(word) if len(list(g)) > 1] # List duplicate letters
335+
>>> [k for k, g in groupby(letters) if len(list(g)) > 1] # List duplicated letters
330336
['a', 'b', 'r']
331337
\end{verbatim}
332338

333-
\item \module{itertools} also gained a function named \function{tee(\var{iterator}, \var{N})} that returns \var{N} independent iterators
334-
that replicate \var{iterator}. If \var{N} is omitted, the default is
335-
2.
339+
\item \module{itertools} also gained a function named
340+
\function{tee(\var{iterator}, \var{N})} that returns \var{N} independent
341+
iterators that replicate \var{iterator}. If \var{N} is omitted, the
342+
default is 2.
336343

337344
\begin{verbatim}
338345
>>> L = [1,2,3]
339346
>>> i1, i2 = itertools.tee(L)
340347
>>> i1,i2
341348
(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
342-
>>> list(i1)
349+
>>> list(i1) # Run the first iterator to exhaustion
343350
[1, 2, 3]
344-
>>> list(i2)
351+
>>> list(i2) # Run the second iterator to exhaustion
345352
[1, 2, 3]
346353
>\end{verbatim}
347354

348355
Note that \function{tee()} has to keep copies of the values returned
349-
by the iterator; in the worst case it may need to keep all of them.
350-
This should therefore be used carefully if \var{iterator}
351-
returns a very large stream of results.
356+
by the iterator; in the worst case, it may need to keep all of them.
357+
This should therefore be used carefully if there the leading iterator
358+
can run far ahead of the trailing iterator in a long stream of inputs.
359+
If the separation is large, then it becomes preferrable to use
360+
\function{list()} instead. When the iterators track closely with one
361+
another, \function{tee()} is ideal. Possible applications include
362+
bookmarking, windowing, or lookahead iterators.
352363

353364
\item A new \function{getsid()} function was added to the
354365
\module{posix} module that underlies the \module{os} module.
@@ -357,26 +368,25 @@ \section{New, Improved, and Deprecated Modules}
357368
\item The \module{operator} module gained two new functions,
358369
\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}.
359370
Both functions return callables that take a single argument and return
360-
the corresponding attribute or item; these callables are handy for use
361-
with \function{map()} or \function{list.sort()}. For example, here's a simple
362-
us
371+
the corresponding attribute or item; these callables make excellent
372+
data extractors when used with \function{map()} or \function{sorted()}.
373+
For example:
363374

364375
\begin{verbatim}
365-
>>> L = [('c', 2), ('d', 1), ('a', '4'), ('b', 3)]
376+
>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
366377
>>> map(operator.itemgetter(0), L)
367378
['c', 'd', 'a', 'b']
368379
>>> map(operator.itemgetter(1), L)
369-
[2, 1, '4', 3]
370-
>>> L.sort(key=operator.itemgetter(1)) # Sort list by second item in tuples
371-
>>> L
372-
[('d', 1), ('c', 2), ('b', 3), ('a', '4')]
380+
[2, 1, 4, 3]
381+
>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
382+
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
373383
\end{verbatim}
374384

375385
\item The \module{random} module has a new method called \method{getrandbits(N)}
376386
which returns an N-bit long integer. This method supports the existing
377387
\method{randrange()} method, making it possible to efficiently generate
378388
arbitrarily large random numbers (suitable for prime number generation in
379-
RSA applications).
389+
RSA applications for example).
380390

381391
\item The regular expression language accepted by the \module{re} module
382392
was extended with simple conditional expressions, written as

0 commit comments

Comments
 (0)