假设我想调试一个带有属性的简单类myattribute
。我创建了一个repr
这样的方法:
class SimpleClass:
def __repr__(self):
return "{0.myattribute}".format(self)
感觉有点多余,所以我宁愿format
直接使用:
class SimpleClass:
__repr__ = "{0.myattribute}".format
...但这失败了IndexError: tuple index out of range
. 我理解它format
无法访问self
论点,但我不明白为什么。
我是不是做错了什么,这是 CPython 的限制——还是别的什么?