[Python-checkins] CVS: python/dist/src/Doc/lib libstdtypes.tex,1.67,1.68
Fred L. Drake
fdrake@users.sourceforge.net
Tue, 28 Aug 2001 07:56:07 -0700
Update of /cvsroot/python/python/dist/src/Doc/lib
In directory usw-pr-cvs1:/tmp/cvs-serv18226/lib
Modified Files:
libstdtypes.tex
Log Message:
Added explanation that [...] * n generates shallow copies of [...], so
the contents will be shared by multiple references.
This closes SF bug #455694.
Index: libstdtypes.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstdtypes.tex,v
retrieving revision 1.67
retrieving revision 1.68
diff -C2 -d -r1.67 -r1.68
*** libstdtypes.tex 2001/08/14 18:22:24 1.67
--- libstdtypes.tex 2001/08/28 14:56:05 1.68
***************
*** 417,421 ****
\hline
\lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
! \lineiii{\var{s} * \var{n}\textrm{,} \var{n} * \var{s}}{\var{n} copies of \var{s} concatenated}{(1)}
\hline
\lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(2)}
--- 417,421 ----
\hline
\lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
! \lineiii{\var{s} * \var{n}\textrm{,} \var{n} * \var{s}}{\var{n} shallow copies of \var{s} concatenated}{(1)}
\hline
\lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(2)}
***************
*** 443,447 ****
\item[(1)] Values of \var{n} less than \code{0} are treated as
\code{0} (which yields an empty sequence of the same type as
! \var{s}).
\item[(2)] If \var{i} or \var{j} is negative, the index is relative to
--- 443,471 ----
\item[(1)] Values of \var{n} less than \code{0} are treated as
\code{0} (which yields an empty sequence of the same type as
! \var{s}). Note also that the copies are shallow; nested structures
! are not copied. This often haunts new Python programmers; consider:
!
! \begin{verbatim}
! >>> lists = [[]] * 3
! >>> lists
! [[], [], []]
! >>> lists[0].append(3)
! >>> lists
! [[3], [3], [3]]
! \end{verbatim}
!
! What has happened is that \code{lists} is a list containing three
! copies of the list \code{[[]]} (a one-element list containing an
! empty list), but the contained list is shared by each copy. You can
! create a list of different lists this way:
!
! \begin{verbatim}
! >>> lists = [[] for i in range(3)]
! >>> lists[0].append(3)
! >>> lists[1].append(5)
! >>> lists[2].append(7)
! >>> lists
! [[3], [5], [7]]
! \end{verbatim}
\item[(2)] If \var{i} or \var{j} is negative, the index is relative to