【发布时间】:2020-05-05 11:30:28
【问题描述】:
所以我遇到的问题是在比较 House 类中的对象时,一个对象是相互的。
def __init__(self, door: str, address: str, rooms: List[room],
garaje=None, number=None):
self.door = door
self.address = address
# ensure and deserialize if needed the list of rooms
try:
if all(isinstance(room, dict) for room in rooms):
rooms = list(map(room.from_json, rooms))
# deserialize rooms
assert all(isinstance(room, Room) for room in rooms)
except AssertionError as e:
logger.error("Rooms incorrect")
raise e
self.rooms = rooms
self.garaje = garaje
self.number = number
def __eq__(self, other):
'''
Two houses instances will be equal when at least following values match:
door,
address
rooms
'''
if not isinstance(other, House):
return NotImplemented
return (self.door == other.door and
self.address == other.address and
self.rooms == other.rooms)
def __hash__(self):
return hash((self.door, self.address, self.rooms))
@classmethod
def from_json(cls, data):
return cls(**data)
House 类包含基于 Room Class 的房间对象列表:
class Room():
def __init__(self, name: str, description: str, windows: list = None, bahtroom: bool = True,
, room_id: int = None, house_number: int = None):
self.name = name
self.description = description
self.windows = windows
self.bathroom = bathroom
self.room_id = room_id
self.house_number = house_number
def __eq__(self, other):
"""Two room instances will be equal when at least following values match
name,
description,
windows,
bathroom
"""
if not isinstance(other, Room):
return NotImplemented
return (self.name == other.name and
self.description == other.description and
self.windows == other.windows and
self.bathroom == other.bathroom
)
def __hash__(self):
return hash((self.name, self.descritpion, self.windows, self.bathroom))
@classmethod
def from_json(cls, data):
return cls(**data)
比较房屋结果时,由于房间列表中的差异,它们不相等。 比较列表中的房间时会有所不同:
自己27间房
[<housing.classes.Room object at 0x7f465f6f83c8>, <housing.classes.Room object at 0x7f465f6f8710>, <housing.classes.Room object at 0x7f465f6f8550>, <housing.classes.Room object at 0x7f465f6f8978>, <housing.classes.Room object at 0x7f465f6f8048>, <housing.classes.Room object at 0x7f465f6f8a20>, <housing.classes.Room object at 0x7f465f6f8518>, <housing.classes.Room object at 0x7f465f6f8898>, <housing.classes.Room object at 0x7f465f6f87b8>, <housing.classes.Room object at 0x7f465f6f84a8>, <housing.classes.Room object at 0x7f465f6f8278>, <housing.classes.Room object at 0x7f465f6f8c50>, <housing.classes.Room object at 0x7f465f6f8940>, <housing.classes.Room object at 0x7f465f6f86a0>, <housing.classes.Room object at 0x7f465f6f8320>, <housing.classes.Room object at 0x7f465f6f8c18>, <housing.classes.Room object at 0x7f465f6f8a58>, <housing.classes.Room object at 0x7f465f6f80b8>, <housing.classes.Room object at 0x7f465f6f8828>, <housing.classes.Room object at 0x7f465f6f8b70>, <housing.classes.Room object at 0x7f465f6f85f8>, <housing.classes.Room object at 0x7f465f6f81d0>, <housing.classes.Room object at 0x7f465f6f8358>, <housing.classes.Room object at 0x7f465f6f8240>, <housing.classes.Room object at 0x7f465f6f8128>, <housing.classes.Room object at 0x7f465f6f8400>, <housing.classes.Room object at 0x7f465f6f8668>]
其他 27 间客房
[<housing.classes.Room object at 0x7f465f6f1470>, <housing.classes.Room object at 0x7f465f6f1978>, <housing.classes.Room object at 0x7f465f6f1f98>, <housing.classes.Room object at 0x7f465f6f1d68>, <housing.classes.Room object at 0x7f465f6f1a58>, <housing.classes.Room object at 0x7f465f6f1cf8>, <housing.classes.Room object at 0x7f465f6f11d0>, <housing.classes.Room object at 0x7f465f6f1438>, <housing.classes.Room object at 0x7f465f6f1898>, <housing.classes.Room object at 0x7f465f6f1ef0>, <housing.classes.Room object at 0x7f465f6f10f0>, <housing.classes.Room object at 0x7f465f6f1198>, <housing.classes.Room object at 0x7f465f6f1240>, <housing.classes.Room object at 0x7f465f6f18d0>, <housing.classes.Room object at 0x7f465f6f17f0>, <housing.classes.Room object at 0x7f465f6f15c0>, <housing.classes.Room object at 0x7f465f6f1828>, <housing.classes.Room object at 0x7f465f6f1518>, <housing.classes.Room object at 0x7f465f6f13c8>, <housing.classes.Room object at 0x7f465f6f1f28>, <housing.classes.Room object at 0x7f465f6f1160>, <housing.classes.Room object at 0x7f465f6f1860>, <housing.classes.Room object at 0x7f465f6f12e8>, <housing.classes.Room object at 0x7f465f6f17b8>, <housing.classes.Room object at 0x7f465f6f1ba8>, <housing.classes.Room object at 0x7f465f6f1400>, <housing.classes.Room object at 0x7f465f6f14a8>]
但是在比较每个房间列表中的房间时,我不断发现self 中的所有房间都存在于other 中。
作为解决方法,我在 __eq__ 内的 House 类中添加了一个讨厌的检查,看起来像这样:
def __eq__(self, other):
'''
Two houses instances will be equal when at least following values match:
door,
address
rooms
'''
if not isinstance(other, House):
return NotImplemented
rooms_from_other = True
rooms_from_self = True
for room in self.rooms:
if room not in other.rooms:
rooms_from_self = False
break
for room in other.rooms:
if room not in self.rooms:
rooms_from_other = False
break
return (self.door == other.door and
self.address == other.address and
rooms_from_other and rooms_from_self)
这样做的好方法是什么,为什么我的房间对象在直接比较但不在列表中时是相等的?
谢谢。
【问题讨论】:
标签: python-3.x object hash compare