为什么 string.Formatter.format 没有 "self" 参数?
Why doesn't string.Formatter.format have a "self" parameter?
在阅读python的string
模块的源代码时,被classFormatter
搞糊涂了。
Formatter class 中的 format
方法(既不是静态方法也不是 class 方法)没有 self
作为输入参数 def format(*args, **kwargs):
,但不知何故直接在方法中使用它。 self, *args = args
.
请解释一下这个用法。
class Formatter:
def format(*args, **kwargs):
if not args:
raise TypeError("descriptor 'format' of 'Formatter' object "
"needs an argument")
self, *args = args # allow the "self" keyword be passed
try:
format_string, *args = args # allow the "format_string" keyword be passed
except ValueError:
if 'format_string' in kwargs:
...
else:
...
return self.vformat(format_string, args, kwargs)
self
假定为*args
中的第一个arg
,并在这一行解包:
self, *args = args
在签名中声明没有 self 的实例方法在 Python 中是不常见的。
通过查看方法签名行的 git history,我们可以看到最初存在 self
。
它被删除是因为如果格式字符串包含名为 self
的变量(例如 'I am my{self}'
),它的存在会导致错误。引入了从 args
解包 self
的不寻常模式来修复错误。
错误报告和讨论是 here。
这是来自错误报告的错误示例:
>>> 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'
我假设您熟悉参数中的 *args
语法。这只是一个任意的未命名参数列表。那么你有
self, *args = args # allow the "self" keyword be passed
该评论非常明确。您将列表 args
拆分为第一个元素(我们通常称为 self
,但它只是一个常规参数,始终是对象方法中的第一个),其余部分。因此,我们读取 self
,一切都很好 - 不是立即,而是在函数中。
我在这里看到的唯一用例来自
if not args:
raise TypeError("descriptor 'format' of 'Formatter' object "
"needs an argument")
这意味着我们希望做类似
的事情
Formatter.format(formatterObj,format_string,...)
很多(不知道为什么,像工厂?),所以如果我们忘记发送 self
- formatterObj
在我的例子中,我们会得到一个详细的错误。可能支持 Formatter
像没有 format
方法但有 vformat
方法的对象。不过似乎不太可能。
在阅读python的string
模块的源代码时,被classFormatter
搞糊涂了。
Formatter class 中的 format
方法(既不是静态方法也不是 class 方法)没有 self
作为输入参数 def format(*args, **kwargs):
,但不知何故直接在方法中使用它。 self, *args = args
.
请解释一下这个用法。
class Formatter:
def format(*args, **kwargs):
if not args:
raise TypeError("descriptor 'format' of 'Formatter' object "
"needs an argument")
self, *args = args # allow the "self" keyword be passed
try:
format_string, *args = args # allow the "format_string" keyword be passed
except ValueError:
if 'format_string' in kwargs:
...
else:
...
return self.vformat(format_string, args, kwargs)
self
假定为*args
中的第一个arg
,并在这一行解包:
self, *args = args
在签名中声明没有 self 的实例方法在 Python 中是不常见的。
通过查看方法签名行的 git history,我们可以看到最初存在 self
。
它被删除是因为如果格式字符串包含名为 self
的变量(例如 'I am my{self}'
),它的存在会导致错误。引入了从 args
解包 self
的不寻常模式来修复错误。
错误报告和讨论是 here。
这是来自错误报告的错误示例:
>>> 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'
我假设您熟悉参数中的 *args
语法。这只是一个任意的未命名参数列表。那么你有
self, *args = args # allow the "self" keyword be passed
该评论非常明确。您将列表 args
拆分为第一个元素(我们通常称为 self
,但它只是一个常规参数,始终是对象方法中的第一个),其余部分。因此,我们读取 self
,一切都很好 - 不是立即,而是在函数中。
我在这里看到的唯一用例来自
if not args:
raise TypeError("descriptor 'format' of 'Formatter' object "
"needs an argument")
这意味着我们希望做类似
的事情Formatter.format(formatterObj,format_string,...)
很多(不知道为什么,像工厂?),所以如果我们忘记发送 self
- formatterObj
在我的例子中,我们会得到一个详细的错误。可能支持 Formatter
像没有 format
方法但有 vformat
方法的对象。不过似乎不太可能。