饱饱 發表於 2019-9-10 22:42:00

python函数 | 函数详解

<h2><span style="font-size: 18pt; font-family: &quot;Microsoft YaHei&quot;"><strong>一、函数初识</strong></span></h2>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">函数的产生:函数就是封装一个功能的代码片段。</span></p>
<div class="cnblogs_code">
<pre>li = [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">spring</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">autumn</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">winter</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> function():
    count </span>=<span style="color: rgba(0, 0, 0, 1)"> 0
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> j <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> li:
      count </span>+= 1
    <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(count)
function()        # 4</span></pre>
</div>
<ul>
<li><strong><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">def 关键字,定义一个函数</span></strong></li>
<li><strong><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">function 函数名的书写规则与变量一样。</span></strong></li>
<li><strong><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">括号是用来传参的。</span></strong></li>
<li><strong><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">函数体,就是函数里面的逻辑代码</span></strong></li>
</ul>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">代码从上至下执行,执行到def function() 时, 将function这个变量名<strong>加载到临时内存中,但它不执行</strong>。</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">函数的执行:函数名 + ()&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>使用</strong><strong>__name_</strong><strong>_方法获取<strong>函数名 ,</strong></strong></span><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>使用</strong><strong>__doc___</strong><strong>方法获取<strong>函数的解释&nbsp;&nbsp;</strong></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    </span><span style="color: rgba(128, 0, 0, 1)">"""</span><span style="color: rgba(128, 0, 0, 1)">
    此函数是完成登陆的功能,参数分别是...作用。
    return: 返回值是登陆成功与否(True,False)
    </span><span style="color: rgba(128, 0, 0, 1)">"""</span>
    <span style="color: rgba(0, 0, 255, 1)">print</span>(666<span style="color: rgba(0, 0, 0, 1)">)

func1()
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(func1.<span style="color: rgba(128, 0, 128, 1)">__name__</span>)         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取函数名</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(func1.<span style="color: rgba(128, 0, 128, 1)">__doc__</span>)         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取函数名注释说明 </span></pre>
</div>
<pre><span>执行输出:
666<span>
func1
此函数是完成登陆的功能,参数分别是...作用。
return: 返回值是登陆成功与否(True,False)</span></span></pre>
<p><span style="text-decoration: underline; font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">这个有什么用呢?比如日志功能,需要打印出谁在什么时间,调用了什么函数,函数是干啥的,花费了多次时间,这个时</span><span style="text-decoration: underline; font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">候,就需要获取函数的有用信息了</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<h3><span style="font-size: 14pt; font-family: &quot;Microsoft YaHei&quot;">1.&nbsp;<strong>函数返回值</strong></span></h3>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;; color: rgba(255, 0, 0, 1)"><strong>写函数,不要在函数中写print()</strong><strong>, &nbsp;</strong><strong>函数是以功能为导向的,除非测试的时候,才可以写print()</strong></span></p>
<ul>
<li><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>在函数中,遇到return结束函数</strong></span></li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> fun():
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(111<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">return</span>
    <span style="color: rgba(0, 0, 255, 1)">print</span>(444<span style="color: rgba(0, 0, 0, 1)">)
fun() </span></pre>
</div>
<pre><span>执行输出:111</span></pre>
<ul>
<li><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>将值返回给函数的调用者</strong></span></li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> fun():
    a </span>= 134
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> a
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(fun()) </span></pre>
</div>
<pre><span>执行输出:123</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">1)<strong>无 return</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> fun():
    </span><span style="color: rgba(0, 0, 255, 1)">pass</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(fun()) </span></pre>
</div>
<pre><span>执行输出:<span style="color: rgba(255, 0, 0, 1)"><strong>None</strong></span></span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">2)<strong>return 1个值</strong><strong>。</strong><strong>该值是什么,就直接返回给函数的调用者,函数名()</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> fun():
    </span><span style="color: rgba(0, 0, 255, 1)">return</span>
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(fun()) </span></pre>
</div>
<pre><span>执行输出:</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">3)<strong>return 多个值</strong> 将多个值<strong>放到一个<span style="color: rgba(255, 0, 0, 1)">元组</span>里</strong>,返回给函数的调用者。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> fun():
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 1,2,,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(fun()) </span></pre>
</div>
<pre><span>执行输出:
(1, 2, , 'abc')</span></pre>
<h3><span style="font-size: 14pt; font-family: &quot;Microsoft YaHei&quot;">2.&nbsp;<strong>函数的传参</strong></span></h3>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong><span style="font-size: 14pt">(1)</span><span style="font-size: 14pt">实参:</span>在函数执行者里面的参数叫实参</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">①<strong>位置参数:按顺序</strong><strong>,</strong><strong>一一对应</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func(a,b,c):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(c)

func(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">fdsafdas</span><span style="color: rgba(128, 0, 0, 1)">'</span>,3,4<span style="color: rgba(0, 0, 0, 1)">) </span></pre>
</div>
<pre><span>执行输出:
fdsafdas
3
4</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong><span style="text-decoration: underline">如果少一个参数呢?</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func(a,b,c):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(c)

func(</span>3,4<span style="color: rgba(0, 0, 0, 1)">) <br></span></pre>
</div>
<pre><span>执行报错:TypeError: func() missing 1 required positional argument: 'c'</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>必须是一一对应的。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> compare(x,y):
    ret </span>=<span style="color: rgba(255, 0, 0, 1)"><strong> x if x &gt; y else y   #三元运算,针对简单的if else才能使用</strong></span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ret

</span><span style="color: rgba(0, 0, 255, 1)">print</span>(compare(123,122334))      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 122334</span></pre>
</div>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">②<strong>关键字参数:可以不按顺序,但是必须一一对应</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> compare(x,y):
    ret </span>= <strong>x <span style="color: rgba(0, 0, 255, 1)">if</span> x &gt; y <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> y
    </span></strong><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ret
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(compare(y=13,x=1<span style="color: rgba(0, 0, 0, 1)">))</span></pre>
</div>
<pre><span>执行结果:13</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">③<strong>混合参数:关键字参数一定要在位置参数后面</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1(a,b,c,d,e):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(c)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(d)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(e)

func1(</span>1,4,d=2,c=3,e=5<span style="color: rgba(0, 0, 0, 1)">) </span></pre>
</div>
<pre><span>执行输出:
1
4
3
2
5</span></pre>
<p><span style="font-size: 14pt"><strong><span style="font-family: &quot;Microsoft YaHei&quot;">(2)&nbsp;形参:</span></strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">①<strong>位置参数:按顺序和实参一一对应,<span style="color: rgba(255, 0, 0, 1)">位置参数必须传值</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func(a,b,c):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(c)

func(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">fdsafdas</span><span style="color: rgba(128, 0, 0, 1)">'</span>,3,4<span style="color: rgba(0, 0, 0, 1)">) </span></pre>
</div>
<pre><span>执行输出:
fdsafdas
3
4</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">②<strong>默认参数:<em><span style="text-decoration: underline">传参则覆盖,不传则默认,默认参数永远在位置参数后面</span></em></strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>例</strong><strong>1.</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(a,b=666<span style="color: rgba(0, 0, 0, 1)">):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a,b)
func(</span>1,2<span style="color: rgba(0, 0, 0, 1)">) </span></pre>
</div>
<pre><span>执行输出:1 2</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>例</strong><strong>2.</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(a,b=666<span style="color: rgba(0, 0, 0, 1)">):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a,b)
func(</span>1<span style="color: rgba(0, 0, 0, 1)">)

执行输出:</span>1 666</pre>
</div>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">举一个场景:班主任录入员工信息表,</span><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">有2个问题:</span><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">第一,男生居多;</span><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">第二,完成函数功能<span style="color: rgba(255, 0, 0, 1)"><strong> *****</strong></span></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span>Infor(username,sex=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">男</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">):
    with open(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name_list</span><span style="color: rgba(128, 0, 0, 1)">'</span>,encoding=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<strong><span style="color: rgba(255, 0, 0, 1)">mode='a'</span></strong><span style="color: rgba(0, 0, 0, 1)">) as f1:
      f1.write(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">{}\t{}\n</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">.format(username,sex))

</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> True:
    username </span>= input(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">请输入姓名(男生以1开头):</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).strip()
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> username:
      username </span>= username      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">去除1</span>
<span style="color: rgba(0, 0, 0, 1)">      Infor(username)
    </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
      Infor(username,</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">女</span><span style="color: rgba(128, 0, 0, 1)">'</span>) </pre>
</div>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>③动态参数</strong>:<strong>当函数的<em><span style="text-decoration: underline">形参数量不一定时</span></em>,可以使用动态参数。用*args和**kwargs接收,args是<span style="text-decoration: underline; color: rgba(255, 0, 0, 1)">元组</span>类型,接收除键值对以外的参数(接收位置参数),kwargs是<span style="text-decoration: underline; color: rgba(255, 0, 0, 1)">字典</span>类型,接收键值对(关键字参数)</strong><strong>,</strong><strong>并保存在字典中。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(*args,**<span style="color: rgba(0, 0, 0, 1)">kwargs):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(args,type(args))
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(kwargs,type(kwargs))

func(</span>1,2,3,4,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">alex</span><span style="color: rgba(128, 0, 0, 1)">'</span>,name = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">alex</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)</span></pre>
</div>
<pre><span>输出结果是:
(1, 2, 3, 4, 'alex') &lt;class 'tuple'&gt;<span>
{'name': 'alex'} &lt;class 'dict'&gt;</span></span></pre>
<p><span style="color: rgba(255, 0, 0, 1)"><strong style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">“ * "的魔性作用</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>(1)在函数定义时:*位置参数和**关键字参数代表聚合</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">将所有实参的位置参数聚合到一个元组中,并将这个元组赋值给args。在关键参数前加“ ** ”代表将实参的关键字参数聚合到一个字典中,并将这个字典赋值给kwargs。</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong><span style="text-decoration: underline">将2个列表的所有元素赋值给args</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(*<span style="color: rgba(0, 0, 0, 1)">args):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(args)

l1 </span>=
l2 </span>=
func(</span>*<span style="color: rgba(0, 0, 0, 1)">l1)
func(</span>*l1,*<span style="color: rgba(0, 0, 0, 1)">l2) </span></pre>
</div>
<pre><span>执行输出:
(1, 2, 30<span>)
(1, 2, 30, 1, 2, 33, 21, 45, 66)</span></span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong><span style="text-decoration: underline">传两个字典给**kwargs</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(**<span style="color: rgba(0, 0, 0, 1)">kwargs):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(kwargs)

dic1 </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">jack</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">age</span><span style="color: rgba(128, 0, 0, 1)">'</span>:22<span style="color: rgba(0, 0, 0, 1)">}
dic2 </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name1</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rose</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">age1</span><span style="color: rgba(128, 0, 0, 1)">'</span>:21<span style="color: rgba(0, 0, 0, 1)">}
func(</span>**dic1,**<span style="color: rgba(0, 0, 0, 1)">dic2) </span></pre>
</div>
<pre><span>执行输出:
{'name': 'jack', 'age': 22, 'name1': 'rose', 'age1': 21}</span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(<strong><span style="color: rgba(255, 0, 0, 1)">*</span></strong>args,<span style="color: rgba(255, 0, 0, 1)"><strong>**</strong></span><span style="color: rgba(0, 0, 0, 1)">kwargs):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(args)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(kwargs)

func(</span><strong><span style="color: rgba(255, 0, 0, 1)">*</span></strong>, <strong><span style="color: rgba(255, 0, 0, 1)">*</span></strong>, <span style="color: rgba(255, 0, 0, 1)"><strong>**</strong></span>{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">alex</span><span style="color: rgba(128, 0, 0, 1)">'</span>},<strong><span style="color: rgba(255, 0, 0, 1)"> **</span></strong>{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">age</span><span style="color: rgba(128, 0, 0, 1)">'</span>:18}) <strong> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">相当于func(, {'name':'alex','age':18</span></strong><strong>})</strong></pre>
<pre></pre>
</div>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>(2)在函数的调用执行时,</strong><strong>打散</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;  *可迭代对象,代表打散(list,tuple,str,dict(键))将元素一一添加到args。</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">   **字典,代表打散,将所有键值对放到一个kwargs字典里。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(<strong><span style="color: rgba(255, 0, 0, 1)">*</span></strong>args,<strong><span style="color: rgba(255, 0, 0, 1)">**</span></strong><span style="color: rgba(0, 0, 0, 1)">kwargs):
    </span><strong><span style="color: rgba(0, 0, 255, 1)">print</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong>(args,kwargs)</strong>

dic1 </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">jack</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">age</span><span style="color: rgba(128, 0, 0, 1)">'</span>:22<span style="color: rgba(0, 0, 0, 1)">}
dic2 </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name1</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rose</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">age1</span><span style="color: rgba(128, 0, 0, 1)">'</span>:21<span style="color: rgba(0, 0, 0, 1)">}

func(</span>*,<span style="color: rgba(255, 0, 0, 1)"><strong>*</strong></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">asdk</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<strong><span style="color: rgba(255, 0, 0, 1)">**</span></strong>dic1,<span style="color: rgba(255, 0, 0, 1)"><strong>**</strong></span><span style="color: rgba(0, 0, 0, 1)">dic2) </span></pre>
</div>
<pre><span style="font-family: &quot;times new roman&quot;, times">执行输出:(1, 2, 3, 4, 'a', 's', 'd', 'k') {'age1': 21, 'name': 'jack', 'age': 22, 'name1': 'rose'}</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;; color: rgba(255, 0, 0, 1)"><strong>形参的顺序:</strong><strong>位置参数 ----&gt; *args -----&gt;关键字参数</strong><strong>--------&gt;默认参数</strong><strong>&nbsp;-------&gt;**kwargs</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>*args参数,可以不传,默认为空(),</strong></span><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>**kwargs 动态传参,他将所有的关键字参数(未定义的)放到一个字典中</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func(a,b,c,d,*args,e=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">男</span><span style="color: rgba(128, 0, 0, 1)">'</span>,**<span style="color: rgba(0, 0, 0, 1)">kwargs):
    </span><span style="color: rgba(51, 153, 102, 1)"><strong>print</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(51, 153, 102, 1)"><strong>(a,b,c,d,args,e,kwargs)</strong></span>

func(</span>1,2,3,4,5,6,7,v=3,m=7,h=9,e=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">女</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) </span></pre>
</div>
<pre><span style="font-family: &quot;times new roman&quot;, times">执行输出:<span style="color: rgba(51, 153, 102, 1)"><strong>1 2 3 4 (5, 6, 7) 女 {'v': 3, 'h': 9, 'm': 7}</strong></span></span></pre>
<div class="cnblogs_code">
<pre>def func(a,b,<strong><span style="color: rgba(255, 0, 0, 1)">c</span></strong>,**<span>kwargs):
    print<span>(kwargs)
func(1,2,r=4,b1=5,c1=6<span style="color: rgba(255, 0, 0, 1)">,c=7</span><span>)

执行输出:{'r': 4, 'c1': 6, 'b1': 5}</span></span></span></pre>
</div>
<p>执行没有报错,是因为函数接收参数后,它会从左边到右找,最后找到了c,c=7参数,<strong>在a,b,c里面已经定义好了,所以在输出的字典中,并未出现。因为kwargs返回的是未定义的关键字参数。</strong></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>如果函数含有多个未知参数,一般使用如下格式:</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> func1(*args,**<span style="color: rgba(0, 0, 0, 1)">kwargs):
    </span><span style="color: rgba(0, 0, 255, 1)">pass</span><span style="color: rgba(0, 0, 0, 1)">
func1() </span></pre>
</div>
<h2><span style="font-size: 18pt; font-family: &quot;Microsoft YaHei&quot;"><strong>二、</strong><strong>命名空间和作用域</strong></span></h2>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>  当执行函数的时候,他会在内存中开辟一个临时名称空间,存放函数体内的所有变量与值的关系,随着函数的执行完毕,临时空间自动关闭。</strong></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>函数里面的变量,在函数外面能直接引用么?</strong><strong>不能</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    m </span>= 1
    <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(m)

</span><span style="color: rgba(0, 0, 255, 1)">print</span>(m)                        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> NameError: name 'm' is not defined</span></pre>
</div>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">上面为什么会报错呢?现在我们来分析一下python内部的原理是怎么样:</span></p>
<p><span style="font-size: 14px; font-family: &quot;Microsoft YaHei&quot;">我们首先回忆一下Python代码运行的时候遇到函数是怎么做的,从Python解释器开始执行之后,就在内存中开辟里一个空间,每当遇到一个变量的时候,就把变量名和值之间对应的关系记录下来,但是当遇到函数定义的时候,<span style="color: rgba(255, 0, 0, 1)"><strong>解释器只是象征性的将函数名读入内存,表示知道这个函数存在了,至于函数内部的变量和逻辑,解释器根本不关心。等执行到函数调用的时候,Python解释器会再开辟一块内存来储存这个函数里面的内容,这个时候,才关注函数里面有哪些变量,而函数中的变量会储存在新开辟出来的内存中,函数中的变量只能在函数内部使用,并且会随着函数执行完毕,这块内存中的所有内容也会被清空。</strong></span></span></p>
<h3><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">1.&nbsp;<strong>命名空间和作用域</strong></span></h3>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>命名空间:存放”名字与值关系的空间“</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">①全局命名空间:代码在运行时,创建的存储”变量名与值的关系“的内存空间</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">②局部命名空间:在函数调用时临时开辟出来的空间,会随着函数的执行完毕而被清空</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">③内置命名空间:存放了python解释器为我们提供的名字:input,print,str,list,tuple...它们都是我们熟悉 的,拿过来就可以用的方法。</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>作用域</strong>:就是作用范围</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">①全局作用域:全局命名空间、内置命名空间。在整个文件的任意位置都能被引用、全局有效</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">②局部作用域:局部命名空间,只能在局部范围内生效</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>加载顺序:</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">内置命名空间(程序运行前加载)-----&gt; &nbsp;&nbsp;全局命名空间(程序运行中从上至下加载) -----&gt; 局部命名空间(程序运行中:调用时才加载)</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>取值顺序:</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">  在局部调用:局部命名空间-&gt;全局命名空间-&gt;内置命名空间</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">  在全局调用:全局命名空间-&gt;内置命名空间</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">综上所述,<strong>在找寻变量时,从小范围,一层一层到大范围去找寻。取值顺序:就近原则</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>局部变量举例</strong></span></p>
<div class="cnblogs_code">
<pre>name = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    name </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">spring</span><span style="color: rgba(128, 0, 0, 1)">'</span>
    <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(name)
func1()</span></pre>
</div>
<pre><span>执行输出:spring<br><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">取值是从内到外</span><br></span></pre>
<div class="cnblogs_code">
<pre>name = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(name)
func1() <br></span></pre>
</div>
<p>&nbsp;<span style="font-family: &quot;Courier New&quot;; font-size: 12px">执行输出:summer</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>代码从上至下依次执行, 调用函数:函数里面从上至下依次执行。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span>(111<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(333<span style="color: rgba(0, 0, 0, 1)">)
    func2()
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(666<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func2():
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(444<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func3():
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(555<span style="color: rgba(0, 0, 0, 1)">)
    func2()

func1()
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(222<span style="color: rgba(0, 0, 0, 1)">) </span></pre>
</div>
<pre><span>执行输出:
111
333
444
666
222</span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> f1():
    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> f2():
      </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> f3():
            </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">in f3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">in f2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      f3()
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">in f1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
    f2()
f1() </span></pre>
</div>
<pre><span style="font-family: &quot;times new roman&quot;, times">执行输出:
in f1
in f2
in f3</span></pre>
<h3><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">2.&nbsp;<strong>global<span style="color: rgba(255, 0, 0, 1)">s</span>和local<span style="color: rgba(255, 0, 0, 1)">s</span>方法</strong></span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span>(globals())       <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">全局名称空间所有变量,字典</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(locals())      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">局部名称空间所有变量,字典 (<strong><span style="color: rgba(255, 0, 0, 1)">当前</span></strong>)</span></pre>
</div>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">globals()和locals()一般很少用,在函数逻辑比较复杂的情况下,可能会用到。</span></p>
<div class="cnblogs_code">
<pre>li = [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">spring</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">autumn</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">winter</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]

</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func():
    a </span>= 1<span style="color: rgba(0, 0, 0, 1)">
    b </span>= 2
    <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">func</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, globals())
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">func</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, locals())

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
      c </span>= 3<span style="color: rgba(0, 0, 0, 1)">
      d </span>= 4
      <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">func1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, globals())
      </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">func1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, locals())

    func1()

func()</span></pre>
</div>
<p>输出结果</p>
<div class="cnblogs_code">
<pre>func {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__name__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__doc__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__package__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__loader__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: &lt;_frozen_importlib_external.SourceFileLoader object at 0x011CC410&gt;, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__spec__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__annotations__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: {}, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__builtins__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: &lt;module <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">builtins</span><span style="color: rgba(128, 0, 0, 1)">'</span> (built-<span style="color: rgba(0, 0, 255, 1)">in</span>)&gt;, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__file__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:/Users/Administrator/houseinfo/test.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__cached__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None,<span style="color: rgba(255, 0, 0, 1)"><strong> 'li':</strong></span> [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">spring</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">autumn</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">winter</span><span style="color: rgba(128, 0, 0, 1)">'</span>], <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">func</span><span style="color: rgba(128, 0, 0, 1)">'</span>: &lt;function func at 0x03542E40&gt;<span style="color: rgba(0, 0, 0, 1)">}
<strong>func {</strong></span><strong><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">b</span><span style="color: rgba(128, 0, 0, 1)">'</span>: 2, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>: 1</strong><span style="color: rgba(0, 0, 0, 1)"><strong>}</strong>
func1 {</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__name__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__doc__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__package__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__loader__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: &lt;_frozen_importlib_external.SourceFileLoader object at 0x011CC410&gt;, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__spec__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__annotations__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: {}, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__builtins__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: &lt;module <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">builtins</span><span style="color: rgba(128, 0, 0, 1)">'</span> (built-<span style="color: rgba(0, 0, 255, 1)">in</span>)&gt;, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__file__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:/Users/Administrator/houseinfo/test.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__cached__</span><span style="color: rgba(128, 0, 0, 1)">'</span>: None, <strong><span style="color: rgba(255, 0, 0, 1)">'li'</span></strong>: [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">spring</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">autumn</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">winter</span><span style="color: rgba(128, 0, 0, 1)">'</span>], <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">func</span><span style="color: rgba(128, 0, 0, 1)">'</span>: &lt;function func at 0x03542E40&gt;<span style="color: rgba(0, 0, 0, 1)">}
<strong>func1 {</strong></span><strong><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">d</span><span style="color: rgba(128, 0, 0, 1)">'</span>: 4, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">'</span>: 3}</strong></pre>
</div>
<p><strong><span style="font-size: 14pt">&nbsp;<span style="font-family: &quot;Microsoft YaHei&quot;">(1)global:</span></span></strong></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>①在局部命名空间</strong><strong><span style="text-decoration: underline">声明</span></strong><strong>全局变量</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func2():
    </span><span style="color: rgba(255, 0, 0, 1)"><strong>global</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong> name</strong></span>
    name </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">summer</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">

func2()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(name)</span></pre>
</div>
<pre><span>执行结果:summer</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>②在局部命名空间对全局变量进行</strong><strong><span style="text-decoration: underline">修改</span></strong><strong>(限于字符串,数字)。</strong></span></p>
<div class="cnblogs_code">
<pre><strong><span style="color: rgba(255, 0, 0, 1)">count = 1</span></strong>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    </span><strong><span style="color: rgba(255, 0, 0, 1)">global</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong><span style="color: rgba(255, 0, 0, 1)"> count</span></strong>
    count </span>= count + 1
    <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(count)
func1()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(count)</span></pre>
</div>
<pre><span>执行结果:<br>2 <br>2</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">因为全局变量count被函数体的global count&nbsp;覆盖了</span></p>
<p><strong><span style="font-size: 14pt; font-family: &quot;Microsoft YaHei&quot;">(2)nonlocal</span></strong></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>①</strong><strong>子函数对父函数的变量进行修改,<span style="color: rgba(255, 0, 0, 1)">此变量不能是全局变量</span></strong></span></p>
<div class="cnblogs_code">
<pre>a = 4
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    nonlocal a
    a </span>= 5             <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">修改全局变量</span>
    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">print(name)</span>
<span style="color: rgba(0, 0, 0, 1)">func1()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a) </span></pre>
</div>
<pre><span>执行输出:SyntaxError: no binding for nonlocal 'a'<span> found</span></span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>②</strong><strong>在局部作用域中,对父级作用域的变量进行引用和修改,并且引用的哪层,从那层及以下此变量全部发生改变。</strong></span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>例</strong><strong>1</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    b </span>= 6
    <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func2():
      b </span>= 666
      <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
    func2()
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(b)               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">父级不受影响</span>
<span style="color: rgba(0, 0, 0, 1)">func1() </span></pre>
</div>
<pre><span>执行输出:
666
6</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>例2</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    b </span>= 6
    <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func2():
<span style="color: rgba(255, 0, 0, 1)"><strong>      nonlocal b          </strong></span></span><span style="color: rgba(255, 0, 0, 1)"><strong>#表示可以影响父级,也就是func1()</strong></span>
      b = 666             <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">重新赋值</span>
      <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
    func2()   
   </span><span style="color: rgba(0, 0, 255, 1)">print</span>(b)               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">这个时候,影响了b的值,输出666</span>
<span style="color: rgba(0, 0, 0, 1)">func1() </span></pre>
</div>
<pre><span>执行输出:
666
666</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>例</strong><strong>3</strong><strong>******</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span> aa():                     <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">不受ccl影响</span>
    b = 42
    <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> bb():
      b </span>= 10               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">影响子级函数,b都是10</span>
      <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
      </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> cc():
            nonlocal b             </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">只能影响父级,也就是bb()</span>
            b = b + 20             <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">b=10+20 也就是30</span>
            <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
      cc()
      </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
    bb()
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(b)
aa()</span></pre>
</div>
<pre><span>执行输出:
10
30
30
42</span></pre>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong>注意</strong></span></p>
<div class="cnblogs_code">
<pre>a = 5
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    a </span>+= 1
    <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a)
func1() </span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">执行报错。</span><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;">这里函数对全局变量做了改变,是不允许操作的。函数内部可以引用全局变量,不能修改。如果要修改,必须要global一下</span></p>
<div class="cnblogs_code">
<pre>a = 5
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> func1():
    </span><span style="color: rgba(0, 0, 255, 1)">global</span><span style="color: rgba(0, 0, 0, 1)"> a
    a </span>+= 1
    <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a)

func1()   </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出6</span></pre>
</div>
<h2>&nbsp;</h2><br><br>
来源:https://www.cnblogs.com/Summer-skr--blog/p/11498293.html
頁: [1]
查看完整版本: python函数 | 函数详解