forked from yasoob/intermediatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs_and_kwargs.html
More file actions
151 lines (136 loc) · 7.75 KB
/
Copy pathargs_and_kwargs.html
File metadata and controls
151 lines (136 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>*args and **kwargs</title>
<link rel="stylesheet" href="_static/epub.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="debugging.html" title="Debugging"
accesskey="N">next</a></li>
<li class="right" >
<a href="index.html" title="Intermediate Python"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Python Tips 0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="args-and-kwargs">
<h1>*args and **kwargs</h1>
<p>I have come to see that most new python programmers have a hard time
figuring out the *args and **kwargs magic variables. So what are they
? First of all let me tell you that it is not necessary to write *args
or **kwargs. Only the <code class="docutils literal"><span class="pre">*</span></code> (aesteric) is necessary. You could have
also written *var and **vars. Writing *args and **kwargs is just a
convention. So now lets take a look at *args first.</p>
<div class="section" id="usage-of-args">
<h2>Usage of *args</h2>
<p>*args and **kwargs are mostly used in function definitions. *args
and **kwargs allow you to pass a variable number of arguments to a
function. What does variable mean here is that you do not know before
hand that how many arguments can be passed to your function by the user
so in this case you use these two keywords. *args is used to send a
<strong>non-keyworded</strong> variable length argument list to the function. Here’s
an example to help you get a clear idea:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test_var_args</span><span class="p">(</span><span class="n">f_arg</span><span class="p">,</span> <span class="o">*</span><span class="n">argv</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"first normal arg:"</span><span class="p">,</span> <span class="n">f_arg</span>
<span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">argv</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"another arg through *argv :"</span><span class="p">,</span> <span class="n">arg</span>
<span class="n">test_var_args</span><span class="p">(</span><span class="s">'yasoob'</span><span class="p">,</span><span class="s">'python'</span><span class="p">,</span><span class="s">'eggs'</span><span class="p">,</span><span class="s">'test'</span><span class="p">)</span>
</pre></div>
</div>
<p>This produces the following result:</p>
<div class="code python highlight-python"><div class="highlight"><pre>first normal arg: yasoob
another arg through *argv : python
another arg through *argv : eggs
another arg through *argv : test
</pre></div>
</div>
<p>I hope this cleared away any confusion that you had. So now lets talk
about **kwargs</p>
</div>
<div class="section" id="usage-of-kwargs">
<h2>Usage of **kwargs</h2>
<p>**kwargs allows you to pass <strong>keyworded</strong> variable length of arguments
to a function. You should use **kwargs if you want to handle <strong>named
arguments</strong> in a function. Here is an example to get you going with it:</p>
<div class="code python highlight-python"><div class="highlight"><pre>def greet_me(**kwargs):
if kwargs is not None:
for key, value in kwargs.iteritems():
print "%s == %s" %(key,value)
>>> greet_me(name="yasoob")
name == yasoob
</pre></div>
</div>
<p>So can you see how we handled a keyworded argument list in our function.
This is just the basics of **kwargs and you can see how useful it is.
Now lets talk about how you can use *args and **kwargs to call a
function with a list or dictionary of arguments.</p>
</div>
<div class="section" id="using-args-and-kwargs-to-call-a-function">
<h2>Using *args and **kwargs to call a function</h2>
<p>So here we will see how to call a function using *args and **kwargs.
Just consider that you have this little function:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test_args_kwargs</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"arg1:"</span><span class="p">,</span> <span class="n">arg1</span>
<span class="k">print</span> <span class="s">"arg2:"</span><span class="p">,</span> <span class="n">arg2</span>
<span class="k">print</span> <span class="s">"arg3:"</span><span class="p">,</span> <span class="n">arg3</span>
</pre></div>
</div>
<p>Now you can use *args or **kwargs to pass arguments to this little
function. Here’s how to do it:</p>
<div class="code python highlight-python"><div class="highlight"><pre># first with *args
>>> args = ("two", 3,5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5
# now with **kwargs:
>>> kwargs = {"arg3": 3, "arg2": "two","arg1":5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
</pre></div>
</div>
<p><strong>Order of using *args **kwargs and formal args</strong></p>
<p>So if you want to use all three of these in functions then the order is</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">some_func</span><span class="p">(</span><span class="n">fargs</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="when-to-use-them">
<h2>When to use them?</h2>
<p>It really depends on what are your requirements. The most common use
case is when making function decorators (discussed in another chapter).
More over it can be used in monkey patching as well. Monkey patching
means modifying some code at runtime. Consider that you have a class
with a function called <code class="docutils literal"><span class="pre">get_info</span></code> which calls an API and returns the
response data. If we want to test it we can replace the API call with
some test data. For instance:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">someclass</span>
<span class="k">def</span> <span class="nf">get_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"Test data"</span>
<span class="n">someclass</span><span class="o">.</span><span class="n">get_info</span> <span class="o">=</span> <span class="n">get_info</span>
</pre></div>
</div>
<p>I am sure that you can think of some other use cases as well.</p>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2015, Muhammad Yasoob Ullah Khalid.
</div>
</body>
</html>