Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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_Ctypes - Fatal编程技术网

Python 我的_eq__;()函数未返回正确的值

Python 我的_eq__;()函数未返回正确的值,python,python-3.x,ctypes,Python,Python 3.x,Ctypes,我有以下代码来构建阵列ADT,但我的\uuuuueq\uuuuuuo()函数不起作用 class Array: def __init__(self, max_capacity): self.array = build_array(max_capacity) self.size = 0 self.index = 0 self.maxsize = max_capacity def __str__(self):

我有以下代码来构建阵列ADT,但我的
\uuuuueq\uuuuuuo()
函数不起作用

class Array:

    def __init__(self, max_capacity):
        self.array = build_array(max_capacity)
        self.size = 0
        self.index = 0
        self.maxsize = max_capacity

    def __str__(self):
        string = "["
        for i in range(self.size):
            string += str(self.array[i])
            string += ', '
        string += ']'
        return string

    def __eq__(self, other):     
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        return False

if __name__ == "__main__":
    test_array = Array(6)

    test_array1 = Array(6)

    print(test_array.__eq__(test_array1))
    print(test_array)
    print(test_array1)
现在,
test\u array.\uuuuueq\uuuuuu(test\u array1)
正在返回
False
当它应该是清晰的
True
时,我甚至正在打印所有内容以确保。我不知道它为什么返回
False
,非常感谢您的帮助

下面是构建数组函数代码

import ctypes


def build_array(size):
    if size <= 0:
        raise ValueError("Array size should be larger than 0.")
    if not isinstance(size,  int):
        raise ValueError("Array size should be an integer.")
    array = (size * ctypes.py_object)()
    array[:] = size * [None]
    return array
导入ctypes
def构建_阵列(大小):

如果size您要求Python比较两个
ctypes
数组(所有其他键值对都是比较相等的对象)

ctypes
数组只有在引用同一对象时才相等

>>> a = build_array(6)
>>> b = build_array(6)
>>> a == b
False
>>> a == a
True
如果它们具有相同的长度并包含相同的元素,则不支持测试。您必须手动执行此操作:

def __eq__(self, other):     
    if not isinstance(other, type(self)):
        return False
    if (self.index != other.index or self.size != other.size or
            self.maxsize != other.maxsize):
        return False
    return all(a == b for a, b in zip(self.array, other.array))

您要求Python比较两个
ctypes
数组(所有其他键值对都是比较相等的对象)

ctypes
数组只有在引用同一对象时才相等

>>> a = build_array(6)
>>> b = build_array(6)
>>> a == b
False
>>> a == a
True
如果它们具有相同的长度并包含相同的元素,则不支持测试。您必须手动执行此操作:

def __eq__(self, other):     
    if not isinstance(other, type(self)):
        return False
    if (self.index != other.index or self.size != other.size or
            self.maxsize != other.maxsize):
        return False
    return all(a == b for a, b in zip(self.array, other.array))

build\u array(6)==build\u array(6)
生成
False
ctypes
数组不支持相等测试。或者换句话说:它们只在引用同一对象时测试为相等。不支持比较它们的内容。
build\u array(6)=build\u array(6)
products
False
ctypes
数组不支持相等测试。或者换句话说:它们只在引用同一对象时测试为相等。不支持比较它们的内容。@eryksun,但在本例中,这将比较指针,因为OP创建了PyObject引用数组。因此,对每个指针进行相等性测试。@eryksun,但在本例中,这将比较指针,因为OP创建了一个PyObject引用数组。因此,对每一个人进行平等性测试。