我希望能夠根據它們的屬性 - 大小來比較 2 個物件。以下是否正確?因為它不起作用
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
謝謝
uj5u.com熱心網友回復:
問題是您需要回傳 booleans :
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 ...
注意。你需要處理所有的可能性
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/362729.html
