【发布时间】:2020-06-05 10:24:36
【问题描述】:
我希望这能工作(在 Python 3.6 中),
class A:
__hash__ = id
A().__hash__()
但我明白了
TypeError: id() takes exactly one argument (0 given)
令人惊讶的是,
def my_id(self):
return id(self)
class A:
__hash__ = my_id
A().__hash__()
按预期工作。
【问题讨论】:
-
不仅仅是
__hash__。用普通方法你会得到同样的结果。我认为id作为一个内置函数,并不完全遵循与用 Python 编写的函数相同的规则。
标签: python class attributes