]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Explain argument unpacking
authorRaymond Hettinger <python@rcn.com>
Fri, 8 Aug 2003 23:32:46 +0000 (23:32 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 8 Aug 2003 23:32:46 +0000 (23:32 +0000)
Doc/tut/tut.tex

index 659ffb7a7de96b332f361c9b0b87788eb1e151e0..0a26d6b37f0f60c8d7e0c524b14d37671a7f592b 100644 (file)
@@ -1577,6 +1577,24 @@ def fprintf(file, format, *args):
 \end{verbatim}
 
 
+\subsection{Unpacking Argument Lists \label{unpacking-arguments}}
+
+The reverse situation occurs when the arguments are already in a list
+or tuple but need to be unpacked for a function call requiring separate
+positional arguments.  For instance, the built-in \function{range()}
+function expects separate \var{start} and \var{stop} arguments.  If they
+are not available separately, write the function call with the 
+\code{*}-operator to unpack the arguments out of a list or tuple:
+
+\begin{verbatim}
+>>> range(3, 6)             # normal call with separate arguments
+[3, 4, 5]
+>>> args = [3, 6]
+>>> range(*args)            # call with arguments unpacked from a list
+[3, 4, 5]
+\end{verbatim}
+
+
 \subsection{Lambda Forms \label{lambda}}
 
 By popular demand, a few features commonly found in functional