在阅读stringpython模块的源代码时,我很困惑Formatter.
formatFormatter类中的方法(不是静态方法或类方法)没有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)
Run Code Online (Sandbox Code Playgroud)