[Python-checkins] python/dist/src/Doc/lib libcollections.tex, 1.8, 1.9

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed May 12 16:56:00 EDT 2004


Update of /cvsroot/python/python/dist/src/Doc/lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9217/Doc/lib

Modified Files:
	libcollections.tex 
Log Message:
Make sure "del d[n]" is properly supported.  Was necessary because the
same method that implements __setitem__ also implements __delitem__.
Also, there were several good use cases (removing items from a queue
and implementing Forth style stack ops).



Index: libcollections.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libcollections.tex,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** libcollections.tex	9 May 2004 01:15:00 -0000	1.8
--- libcollections.tex	12 May 2004 20:55:56 -0000	1.9
***************
*** 138,159 ****
  slicing and deletion:
  
  \begin{verbatim}
  def delete_nth(d, n):
-     "del d[n]"
      d.rotate(-n)
      d.popleft()
      d.rotate(n)
- 
- >>> d = deque('abcdef')
- >>> delete_nth(d, 2)   # remove the entry at d[2]
- >>> d
- deque(['a', 'b', 'd', 'e', 'f'])
- 
  \end{verbatim}
  
! For slicing, the idea is the same.  Use \method{rotate()} to bring a target
! element to the left side of the deque.  Remove old entries with
! \method{popleft()}, add new entries with \method{extend()}, and then
! reverse the rotation.
  
  With minor variations on that approach, it is easy to implement Forth style
--- 138,156 ----
  slicing and deletion:
  
+ This pure python implementation of \code{del d[n]} shows how to use the
+ \method{rotate()} method as a building block for implementing a variety
+ of class{deque} operations:
+ 
  \begin{verbatim}
  def delete_nth(d, n):
      d.rotate(-n)
      d.popleft()
      d.rotate(n)
  \end{verbatim}
  
! To implement \class{deque} slicing, use a similar approach applying
! \method{rotate()} to bring a target element to the left side of the deque.
! Remove old entries with \method{popleft()}, add new entries with
! \method{extend()}, and then reverse the rotation.
  
  With minor variations on that approach, it is easy to implement Forth style




More information about the Python-checkins mailing list