<!-- language: lang-py -->
class File:
### Сlass initialization
def __init__(self, path, name, size, date):
self.path = path
self.name = name
self.size = size
self.date = date
def __eq__(self, other):
# if self.name == other.name and self.size == other.size and self.date == other.date:
if self.name == other.name and self.size == other.size:
# if self.size == other.size and self.date == other.date:
return True**
def __eq__(self, other):
# if self.name == other.name and self.size == other.size and self.date == other.date:
if self.name == other.name and self.size == other.size:
# if self.size == other.size and self.date == other.date:
return True
当某些情况发生时,必须触发不同的变体
与其动态地交换
__eq__
,为什么不使用条件来确定调用__eq__
时使用哪种情况当然,这是可能的:
说明:
__eq__
是类Foo
的属性,是绑定到类的函数(类方法)。可以将__eq__
属性设置为新函数来替换它。注意,因为这是在修改类,所以所有实例都会看到更改,包括已经实例化的foo1
和foo2
尽管如此,这是一个相当粗略的实践,特别是对于
__eq__
这样的东西,所以我想说这可能不是一个很好的解决问题的方法,但是不知道问题是什么,我只想说如果我在代码中看到这类东西,会让我相当紧张相关问题 更多 >
编程相关推荐