Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在脚本执行期间更改类的eq_Python_Python 3.x - Fatal编程技术网

Python 如何在脚本执行期间更改类的eq

Python 如何在脚本执行期间更改类的eq,python,python-3.x,Python,Python 3.x,当某些情况发生时,必须触发不同的变量,而不是动态地交换\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,为什么不使用这些条件 def __eq__(self, other): # if self.name == other.name and self.size == other.size and self.date == other.

当某些情况发生时,必须触发不同的变量,而不是动态地交换
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,为什么不使用这些条件

    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

这当然是可能的:

class Foo:
    def __eq__(self, other):
        if (self._condition_1):
            return self._eq_condition_1(other)
        elif (self._condition_2):
            return self._eq_condition_2(other)
        else:
            return self._eq_condition_default(other)

    def _eq_condition_1(self, other):
        return True

    def _eq_condition_2(self, other):
        return False

    def _eq_condition_default(self, other):
        return True
说明:

\uuuu eq\uuu
是类
Foo
的属性,是绑定到类的函数(类方法)。您可以将
\uuuu eq\uuu
属性设置为新函数以替换它。请注意,因为这是在修改类,所以所有实例都会看到更改,包括已实例化的
foo1
foo2


总而言之,这是一个相当粗略的实践,特别是对于像
\uuuuueq\uuuuuu
这样的东西,所以我想说,这可能不是解决问题的好办法,但不知道问题是什么,我只想说,如果我在代码中看到这种东西,我会非常紧张。

这是一个XY问题;在程序中间更改
\uuuuu eq\uuuuuu
方法几乎肯定不是解决您试图解决的任何问题的最佳方法。我没有使用它来查看您的问题是否可行,但我认为更正确的解决方案是使用
\uuuuuu eq\uuuu
根据条件路由到不同的功能。当触发修改时,在
\uuuuuuueq\uuu
调用?闻起来好像你需要一个工厂模式。在什么条件下你希望
\uuuueq\uuuuueq
的结果发生变化?当我需要使用一个代码比较一些文件时,只需更改“==”。如果prog没有找到“完整”副本(名称、时间、大小),我需要更改“==”以按时间和大小检查副本。我肯定我们还有别的办法,但我找不到。谢谢。我需要比较一些文件,找到重复的。我知道可以用另一种方式来做,但对我来说(非常年轻的)这很有趣。对不起,我的英语水平很低
class Foo:
    def __eq__(self, other):
        if (self._condition_1):
            return self._eq_condition_1(other)
        elif (self._condition_2):
            return self._eq_condition_2(other)
        else:
            return self._eq_condition_default(other)

    def _eq_condition_1(self, other):
        return True

    def _eq_condition_2(self, other):
        return False

    def _eq_condition_default(self, other):
        return True
class Foo(object):
    def __init__(self, x):
        self.x = x

    def __eq__(self, other):
        return other.x == self.x

foo1 = Foo(1)
foo2 = Foo(2)

print (foo1 == foo2)

def new_eq(self, other):
    return other.x - 1 == self.x

Foo.__eq__ = new_eq

print (foo1 == foo2)