小编use*_*797的帖子

为什么string.Formatter.format没有"self"参数?

在阅读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)

python string class self

3
推荐指数
1
解决办法
97
查看次数

标签 统计

class ×1

python ×1

self ×1

string ×1