字符串比较__Lt_运算符python:



我希望能够根据两个对象的属性Size来比较它们。以下内容正确吗?因为它不工作

def __lt__(self, other): 
if self.size == "small" and other.size == "big":
return other.size > self.size
if self.size == "small" and other.size == "medium":
return other.size > self.size
if self.size == "medium" and other.size == "big":
return other.size > self.size

感谢

问题是您需要返回布尔值:

def __lt__(self, other): 
if self.size == "small" and other.size in ["big", "medium"]:
return True
if self.size == "medium" and other.size == "large":
return True
if ...

注:。你需要处理所有的可能性

最新更新