<!-- flowchart 箭头图标 勿删 -->
<svg style="display: none;">
<path d="M5,0 0,2.5 5,5z" id="raphael-marker-block" stroke-linecap="round" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p>对于函数的了解还是不够透彻,毕竟有很多小细节需要注意。最简单的部分我们不再赘述,主要讲一下需要注意的几点。</p>
<h1 id="1函数参数">1.函数参数</h1>
<h2 id="11函数参数种类">1.1函数参数种类</h2>
<p>函数参数主要分为3种:正常参数,默认参数,可变长参数。 <br>
所谓正常参数,就是我们常规意义上的参数,就和f(x)一样,没什么区别。例如:</p>
<pre class="blockcode"><code class="language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">power</span><span class="hljs-params">(x, n)</span>:</span>
s = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> n > <span class="hljs-number">0</span>:
n = n - <span class="hljs-number">1</span>
s = s * x
<span class="hljs-keyword">return</span> s</code></pre>
<p>而默认参数则是指具有默认值的参数。例如:</p>
<pre class="blockcode"><code class="language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">power</span><span class="hljs-params">(x, n=<span class="hljs-number">2</span>)</span>:</span>
s = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> n > <span class="hljs-number">0</span>:
n = n - <span class="hljs-number">1</span>
s = s * x
<span class="hljs-keyword">return</span> s</code></pre>
<p>可以看到和正常参数不同的是,默认参数n有了初试值2,那么再进行调用的时候,就可以这样调用:</p>
<pre class="blockcode"><code class="language-python hljs"><span class="hljs-prompt">>>> </span>power(<span class="hljs-number">5</span>)
<span class="hljs-number">25</span>
<span class="hljs-prompt">>>> </span>power(<span class="hljs-number">5</span>, <span class="hljs-number">2</span>)
<span class="hljs-number">25</span></code></pre>
<p>这两种效果是等同的。但是在默认参数传递时,需要注意2点: <br>
1. 默认参数必须是在参数的最后,不然的话,计算机并不知道你接下来的参数是默认参数还是正常参数。 <br>
2. 默认参数一定不能够在函数中发生改变,否则的话会非常奇怪。例如:</p>
<pre class="blockcode"><code class="language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_end</span><span class="hljs-params">(L=[])</span>:</span>
L.append(<span class="hljs-string">'END'</span>)
<span class="hljs-keyword">return</span> L
<span class="hljs-prompt">>>> </span>add_end()
[<span class="hljs-string">'END'</span>]
<span class="hljs-prompt">>>> </span>add_end()
[<span class="hljs-string">'END'</span>, <span class="hljs-string">'END'</span>]
<span class="hljs-prompt">>>> </span>add_end()
[<span class="hljs-string">'END'</span>, <span class="hljs-string">'END'</span>, <span class="hljs-string">'END'</span>]</code></pre>
<p>Python函数在定义的时候,默认参数L的值就被计算出来了,即[],因为默认参数L也是一个变量,它指向对象[],每次调用该函数,如果改变了L的内容,则下次调用时,默认参数的内容就变了,不再是函数定义时的[]了。</p>
<p>如果一定要避免这个情况的话,必须这么写(虽然这真的是为了这么写而这么写,不建议):</p>
<pre class="blockcode"><code class="language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_end</span><span class="hljs-params">(L=None)</span>:</span>
<span class="hljs-keyword">if</span> L <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
L = []
L.append(<span class="hljs-string">'END'</span>)
<span class="hljs-keyword">return</span> L</code></pre>
<p>第三个可变长的参数分为2种,一种是<code>*t</code>,另一种是<code>**d</code>。</p>
<p><code>*t</code>参数是元组参数,接受任意多个实参并将其放在一个元组中。 <br>
例如:</p>
<pre class="blockcode"><code class="language-phthon hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">myvar1</span><span class="hljs-params">(*t)</span>:</span>
print(t)
myvar1(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)
myvar1(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>)
<span class="hljs-comment">#程序输出结果如下:</span>
<span class="hljs-comment">#(1, 2, 3)</span>
<span class="hljs-comment">#(1, 2, 3, 4, 5)</span></code></pre>
<p><code>**d</code>参数则是字典参数,可以接受任意多个实参。实参的形式为:关键字=实参值。在字典可变长度参数中,关键字参数和实参值参数被 |
|