擅长:python、mysql、java
<p><code>self</code>假定为<code>*args</code>中的第一个<code>arg</code>,并在此行中展开:</p>
<p><code>self, *args = args</code></p>
<p>在签名中声明一个没有self的实例方法在Python中是不常见的。你知道吗</p>
<p>通过查看方法签名行的<a href="https://github.com/python/cpython/blame/be1eb1424121682081fed8996e87e2dfd238e7ea/Lib/string.py#L163" rel="nofollow noreferrer">git history</a>,我们可以看到最初的<code>self</code>是存在的。你知道吗</p>
<p>它已被删除,因为如果格式字符串包含名为<code>self</code>的变量(例如<code>'I am my{self}'</code>),则它的存在会导致错误。引入了从<code>args</code>解包<code>self</code>的不寻常模式来修复这个bug。你知道吗</p>
<p>错误报告和讨论是<a href="https://bugs.python.org/issue23671" rel="nofollow noreferrer">here</a>。你知道吗</p>
<p>这是错误报告中的错误示例:</p>
<pre><code>>>> string.Formatter().format('the self is {self}', self='bozo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: format() got multiple values for argument 'self'
</code></pre>