擅长:python、mysql、java
<p>你所说的似乎是完全正确的,即使它似乎偏离了你的预期</p>
<p>通常,您可以将其视为,当函数末尾有圆括号时,即<code>example()</code>,函数被执行</p>
<p>最后一个问题有点含糊,但是您可以在函数中使用<code>return</code>关键字,在某个时候停止执行该函数。这在执行一些资源密集型计算的函数中是有意义的,但偶尔也有机会走捷径</p>
<p>举个例子</p>
<pre><code>def calculate_thing(shortcut = False):
if shortcut:
return 3
# Resource-intensive, time-consuming calculations go here
return result_of_calculations
</code></pre>
<p>用<code>calculate_thing(shortcut=True)</code>调用此函数将很快返回<code>3</code>,因为当我们点击<code>return 3</code>时,函数将停止执行。另一方面,通过<code>calculate_thing(shortcut=False)</code>或<code>calculate_thing()</code>(<code>False</code>是<code>shortcut</code>的默认值)调用它将使函数运行一段时间,进行一些计算,然后返回分配给变量<code>result_of_calculations</code>的任何值</p>