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

Skip to content

Commit 2337f55

Browse files
committed
Mention setdefault() method for dicts
1 parent c756bdb commit 2337f55

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Doc/whatsnew/whatsnew20.tex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,22 @@ \section{Core Changes}
556556
\var{level} is a string such as \code{"alpha"}, \code{"beta"}, or
557557
\code{""} for a final release.
558558
559+
Dictionaries have an odd new method, \method{setdefault(\var{key},
560+
\var{default})}, which behaves similarly to the existing
561+
\method{get()} method. However, if the key is missing,
562+
\method{setdefault()} both returns the value of \var{default} as
563+
\method{get()} would do, and also inserts it into the dictionary as
564+
the value for \var{key}. Thus, the following lines of code:
565+
566+
\begin{verbatim}
567+
if dict.has_key( key ): return dict[key]
568+
else:
569+
dict[key] = []
570+
return dict[key]
571+
\end{verbatim}
572+
573+
can be reduced to a single \code{return dict.setdefault(key, [])} statement.
574+
559575
% ======================================================================
560576
\section{Extending/Embedding Changes}
561577

0 commit comments

Comments
 (0)