class Element:
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return repr(self.value)
example = Element("project", "some project")
example == "some project" # False
有没有办法做出上述声明True
而不是
example.value == "some project" # True
最佳答案
您可以尝试在类中重载 __eq__
和 __ne__
运算符。
class Element:
def __init__(self, val):
self.val = val
def __eq__(self, other):
return self.val == other
f = Element("some project")
f == "some project" # True
关于python - 是否可以在 python 中使 Instance == string/int/float 或任何数据类型为 True ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62464668/