对于我的编码项目,我必须使用这段代码,看看是否有类似的属性。如果是的话, 我需要一个print语句说“列表具有相似的属性”,如果没有,那么应该有一个print语句说“这两个语句具有相同的属性”
class Box:
def __init__(self, date1, contents1, location1):
self.date = date1
self.contents = contents1
self.location = location1
box23 = Box("2016", "medical records", "storage closet")
box21 = Box("2018", "lotion samples", "waiting room")
box07 = Box("2020", "flyers for flu shot", "receptionist desk")
print(box23.date)
print(box21.contents)
print(box07.location)
如果要获取两个对象上的所有属性:
->;返回所有匹配属性的列表
如果要获取位于对象和的所有属性也具有相同的值:
确保您还获得了上面定义的函数
get_matching_attributes
,以便使用->;返回也具有相同值的所有匹配属性的列表
提示:如果要获取tuble中所有匹配属性的列表,请将最后一列更改为
return matching_values
,其中True
或False
指示属性在两个对象中是否具有相同的值如果要比较单个属性,可以使用
如果要比较所有属性,只需比较
__dict__
属性即可:相关问题 更多 >
编程相关推荐