Update of /cvsroot/py-howto/pyhowto
In directory slayer.i.sourceforge.net:/tmp/cvs-serv17585
Modified Files:
python-2.0.tex
Log Message:
Mention setdefault() method for dicts
Index: python-2.0.tex
===================================================================
RCS file: /cvsroot/py-howto/pyhowto/python-2.0.tex,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -r1.21 -r1.22
*** python-2.0.tex 2000/08/04 12:40:35 1.21
--- python-2.0.tex 2000/08/16 02:52:37 1.22
***************
*** 557,560 ****
--- 557,576 ----
\code{""} for a final release.
+ Dictionaries have an odd new method, \method{setdefault(\var{key},
+ \var{default})}, which behaves similarly to the existing
+ \method{get()} method. However, if the key is missing,
+ \method{setdefault()} both returns the value of \var{default} as
+ \method{get()} would do, and also inserts it into the dictionary as
+ the value for \var{key}. Thus, the following lines of code:
+
+ \begin{verbatim}
+ if dict.has_key( key ): return dict[key]
+ else:
+ dict[key] = []
+ return dict[key]
+ \end{verbatim}
+
+ can be reduced to a single \code{return dict.setdefault(key, [])} statement.
+
% ======================================================================
\section{Extending/Embedding Changes}
|