-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargumentos-somente-por-palavra-chave.html
352 lines (302 loc) · 17.1 KB
/
argumentos-somente-por-palavra-chave.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!DOCTYPE html>
<html lang="pt" prefix="og: http://ogp.me/ns# fb: https://www.facebook.com/2008/fbml">
<head>
<title>Argumentos somente por palavra chave - import None</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="http://cassiobotaro.github.io/argumentos-somente-por-palavra-chave.html">
<meta name="author" content="Cássio Botaro" />
<meta name="keywords" content="python3" />
<meta name="description" content="Como um novo recurso do python 3 te ajuda a não brincar de roleta russa com seus parâmetros." />
<meta property="og:site_name" content="import None" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="Argumentos somente por palavra chave"/>
<meta property="og:url" content="http://cassiobotaro.github.io/argumentos-somente-por-palavra-chave.html"/>
<meta property="og:description" content="Como um novo recurso do python 3 te ajuda a não brincar de roleta russa com seus parâmetros."/>
<meta property="article:published_time" content="2015-03-28" />
<meta property="article:section" content="Python3" />
<meta property="article:tag" content="python3" />
<meta property="article:author" content="Cássio Botaro" />
<!-- Bootstrap -->
<link rel="stylesheet" href="http://cassiobotaro.github.io/theme/css/bootstrap.united.min.css" type="text/css"/>
<link href="http://cassiobotaro.github.io/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="http://cassiobotaro.github.io/theme/css/pygments/monokai.css" rel="stylesheet">
<link rel="stylesheet" href="http://cassiobotaro.github.io/theme/css/style.css" type="text/css"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="http://cassiobotaro.github.io/" class="navbar-brand">
import None </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li >
<a href="http://cassiobotaro.github.io/category/linux.html">Linux</a>
</li>
<li >
<a href="http://cassiobotaro.github.io/category/python.html">Python</a>
</li>
<li class="active">
<a href="http://cassiobotaro.github.io/category/python3.html">Python3</a>
</li>
<li >
<a href="http://cassiobotaro.github.io/category/sublime.html">Sublime</a>
</li>
<li >
<a href="http://cassiobotaro.github.io/category/web2py.html">Web2py</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="http://cassiobotaro.github.io/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archives</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="http://cassiobotaro.github.io/argumentos-somente-por-palavra-chave.html"
rel="bookmark"
title="Permalink to Argumentos somente por palavra chave">
Argumentos somente por palavra chave
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2015-03-28T19:00:00-03:00"> Sáb 28 março 2015</time>
</span>
<span class="label label-default">Tags</span>
<a href="http://cassiobotaro.github.io/tag/python3.html">python3</a>
</footer><!-- /.post-info --> </div>
</div>
<h2>Por que falar sobre isto?</h2>
<p>Python 3 ainda possui uma névoa sobre ele, e o movimento de resistência ainda é grande. Mas isto não quer dizer que desbravadores já não o estejam explorando. Já escrevi um <a href="http://cassiobotaro.github.io/porque-python-3.html">post</a> sobre a nova versão da linguagem, porém ainda tem vários recursos que podem ser demonstrados. Um deles é a capacidade de aceitar somente palavras chaves como argumento. Eu até coloquei isto nos slides da Python Brasil 10, porém senti que ao apresentar faltou uma boa explicação.</p>
<h2>Talk is cheap. Show me the code</h2>
<p>Tentarei dar um exemplo utilizando código, explicarei utilizando os comentários</p>
<div class="highlight"><pre><span class="c"># vamos iniciar com a seguinte função</span>
<span class="k">def</span> <span class="nf">funcao</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">kill</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">kill</span><span class="p">:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="c"># realiza operacao</span>
<span class="o">...</span>
<span class="c"># e por algum algum descuido...</span>
<span class="n">funcao</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="c"># o que acontece?</span>
<span class="c"># possivelmente você acaba de ser demitido</span>
<span class="c"># Agora vamos corrigir isto utilizando python3</span>
<span class="c"># lembre-se que daqui pra baixo, a sintáxe não é compativel com python 2</span>
<span class="k">def</span> <span class="nf">funcao</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="o">*</span><span class="p">,</span> <span class="n">kill</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">kill</span><span class="p">:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="c"># realiza operacao</span>
<span class="o">...</span>
<span class="c"># Basta colocar um * e pronto</span>
<span class="c"># agora se cometer um erro o python 3 te alerta</span>
<span class="n">funcao</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="c"># TypeError: funcao() takes 2 positional arguments but 3 were given</span>
<span class="c"># Mas o que é esse asterico, é igual o args?</span>
<span class="c"># Na verdade é similar, porém o interpretador sabe que se não foi definido o </span>
<span class="c"># "args" um erro deve ser lançado</span>
<span class="c">#Mas e se eu definir o args?</span>
<span class="k">def</span> <span class="nf">funcao</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="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">kill</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">kill</span><span class="p">:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="c"># realiza operacao</span>
<span class="o">...</span>
<span class="c"># acontece que agora, todo parâmetro passado além dos dois obrigatórios(positionals), será armazenado em args, e kill só sera atribuido quando</span>
<span class="c"># utilizado palavra chave</span>
<span class="c">#Ex: funcao(0, 0, kill=True)</span>
</pre></div>
<h2>Considerações finais</h2>
<p>Não pense que exemplo é exagerado, pode acontecer a qualquer hora com você.
Não brinque mais de roleta russa com seus argumentos.</p>
<p><img alt="programming-motherfucker" src="../images/programming-motherfucker.jpg" /></p>
</div>
<!-- /.entry-content -->
<hr />
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
</div>
<!-- AddThis Button END -->
<hr/>
<section class="comments" id="comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'importnone'; // required: replace example with your forum shortname
var disqus_identifier = 'argumentos-somente-por-palavra-chave';
var disqus_url = 'http://cassiobotaro.github.io/argumentos-somente-por-palavra-chave.html';
var disqus_config = function () {
this.language = "pt";
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<div id="aboutme">
<p>
<img width="100%" class="img-thumbnail" src="http://www.gravatar.com/avatar/a19c2dcbc9fc6ae1c340528186320d9c?s=100"/>
</p>
<p>
<strong>About Cássio Botaro</strong><br/>
WebDeveloper at Diarios Associados. UHlzc2lvbmF0ZSB3ZWJkZXZlbG9wZXIuCg==
</p>
</div>
<section class="well well-sm">
<ul class="list-group list-group-flush">
<li class="list-group-item"><h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="https://github.com/cassiobotaro"><i class="fa fa-github-square fa-lg"></i> github</a></li>
<li class="list-group-item"><a href="http://twitter.com/cassiobotaro"><i class="fa fa-twitter-square fa-lg"></i> twitter</a></li>
<li class="list-group-item"><a href="https://plus.google.com/u/0/+C%C3%A1ssioBotaro"><i class="fa fa-google-plus-square fa-lg"></i> google-plus</a></li>
<li class="list-group-item"><a href="http://br.linkedin.com/pub/c%C3%A1ssio-botaro/b1/43/6a2"><i class="fa fa-linkedin-square fa-lg"></i> linkedin</a></li>
</ul>
</li>
<li class="list-group-item"><a href="http://cassiobotaro.github.io/"><h4><i class="fa fa-tags fa-lg"></i><span class="icon-label">Tags</span></h4></a>
<ul class="list-group list-inline tagcloud" id="tags">
</ul>
</li>
<li class="list-group-item"><h4><i class="fa fa-github fa-lg"></i><span class="icon-label">GitHub Repos</span></h4>
<div id="gh_repos">
<p class="list-group-item">Status updating...</p>
</div>
<a href="https://github.com/cassiobotaro">@cassiobotaro</a> on GitHub
</li>
<li class="list-group-item"><h4><i class="fa fa-external-link-square fa-lg"></i><span class="icon-label">Links</span></h4>
<ul class="list-group" id="links">
<li class="list-group-item">
<a href="http://getpelican.com/" target="_blank">
Pelican
</a>
</li>
<li class="list-group-item">
<a href="http://python.org/" target="_blank">
Python.org
</a>
</li>
<li class="list-group-item">
<a href="http://jinja.pocoo.org/" target="_blank">
Jinja2
</a>
</li>
</ul>
</li>
</ul>
</section>
</aside>
</div>
</div>
</div>
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2015 Cássio Botaro
· Powered by <a href="https://github.com/DandyDev/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> </div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="http://cassiobotaro.github.io/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="http://cassiobotaro.github.io/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="http://cassiobotaro.github.io/theme/js/respond.min.js"></script>
<!-- GitHub JS -->
<script type="text/javascript">
$(document).ready(function () {
if (!window.jXHR) {
var jxhr = document.createElement('script');
jxhr.type = 'text/javascript';
jxhr.src = 'http://cassiobotaro.github.io/theme/js/jXHR.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(jxhr, s);
}
github.showRepos({
user: 'cassiobotaro',
count: 3,
skip_forks: true,
target: '#gh_repos'
});
});
</script>
<script src="http://cassiobotaro.github.io/theme/js/github.js" type="text/javascript"></script>
<!-- End GitHub JS Code -->
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'importnone'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-59964005-1']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics Code -->
<script type="text/javascript">var addthis_config = {"data_track_addressbar": true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-54023148366c6bdd"></script>
</body>
</html>