diff options
author | Andrew M. Kuchling <amk@amk.ca> | 2000-08-16 02:52:37 (GMT) |
---|---|---|
committer | Andrew M. Kuchling <amk@amk.ca> | 2000-08-16 02:52:37 (GMT) |
commit | 2337f5519aa96dbd8c750da9ecaf0b06627ad1d6 (patch) | |
tree | 5fc39549ecd58fb602c231ed3bb418247fecc512 /Doc/whatsnew | |
parent | c756bdb66c1db26271166bee0d5de5eb733e270c (diff) | |
download | cpython-2337f5519aa96dbd8c750da9ecaf0b06627ad1d6.zip cpython-2337f5519aa96dbd8c750da9ecaf0b06627ad1d6.tar.gz cpython-2337f5519aa96dbd8c750da9ecaf0b06627ad1d6.tar.bz2 |
Mention setdefault() method for dicts
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r-- | Doc/whatsnew/whatsnew20.tex | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Doc/whatsnew/whatsnew20.tex b/Doc/whatsnew/whatsnew20.tex index 9211e64..7cc9913 100644 --- a/Doc/whatsnew/whatsnew20.tex +++ b/Doc/whatsnew/whatsnew20.tex @@ -556,6 +556,22 @@ added to the \module{sys} module. \code{sys.version_info} is a tuple \var{level} is a string such as \code{"alpha"}, \code{"beta"}, or \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} |