擅长:python、mysql、java
<p>在最新的Python版本(<a href="https://docs.python.org/3.8/whatsnew/3.8.html" rel="nofollow noreferrer">Python 3.8</a>)中,有一个新的赋值可能对您有用,<code>:=</code>:</p>
<blockquote>
<p>There is new syntax <code>:=</code> that assigns values to variables as part of a larger expression. It is affectionately known as “walrus operator” due to its resemblance to the eyes and tusks of a walrus.</p>
</blockquote>
<pre><code>if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
</code></pre>
<blockquote>
<p>In this example, the assignment expression helps avoid calling len() twice:</p>
</blockquote>