Python Deepcopy产量“;返回了一个错误集为的结果”;使用自定义散列函数

Python Deepcopy产量“;返回了一个错误集为的结果”;使用自定义散列函数,python,numpy,hash,python-3.8,deep-copy,Python,Numpy,Hash,Python 3.8,Deep Copy,我正在尝试对以下对象的数组进行深度复制位置: class Location(object): def __init__(self, row: int, col: int): self.__pos_row = row # col self.__pos_col = col # row self.__neighbors = None self.is_wall = None self._hash = None

我正在尝试对以下对象的数组进行深度复制
位置

class Location(object):
    def __init__(self, row: int, col: int):
        self.__pos_row = row  # col
        self.__pos_col = col  # row
        self.__neighbors = None
        self.is_wall = None
        self._hash = None

    def __hash__(self):
        if self._hash is None:
            prime = 31
            _hash = 1
            _hash = _hash * prime + hash(self.__str__())
            _hash = _hash * prime + hash(tuple((self.__pos_row, self.__pos_col)))
            self._hash = _hash
        return self._hash

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        return 'L{},{}'.format(
            self.__pos_row,
            self.__pos_col
        )

    def __eq__(self, value):
        if not isinstance(value, Location):
            raise Exception(
                'Cannot compare Location with %s.' % type(value)
            )
        
        if (self.row != value.row) or (self.col != value.col):
            return False
        
        return True
以下是如何为Level对象构建位置数组:

layout = np.array([[Location(row, col) for col in range(1, col_cnt -1)] for row in range(1, row_cnt - 1)], dtype=object)
但是,调用
level.clone()
会产生以下错误:

Traceback (most recent call last):
  File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.8/copy.py", line 264, in _reconstruct
    y = func(*args)
  File "/usr/lib/python3.8/copy.py", line 263, in <genexpr>
    args = (deepcopy(arg, memo) for arg in args)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)

  [...]

  File "/usr/lib/python3.8/copy.py", line 264, in _reconstruct
    y = func(*args)
  File "/home/ai-final/bin/location.py", line 15, in __hash__
    if self._hash is None:

  AttributeError: 'Location' object has no attribute '_hash'
    
  The above exception was the direct cause of the following exception:
    
  Traceback (most recent call last):
    File "/usr/lib/python3.8/copy.py", line 137, in deepcopy
      d = id(x)
  SystemError: <built-in function id> returned a result with an error set
    
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File "/usr/lib/python3.8/copy.py", line 137, in deepcopy
      d = id(x)
  SystemError: <built-in function id> returned a result with an error set

  [...]

  Traceback (most recent call last):
    File "/home/ai-final/bin/level.py", line 38, in copy
      copy.deepcopy(__layout),
    File "/usr/lib/python3.8/copy.py", line 153, in deepcopy
      y = copier(memo)
  SystemError: <built-in method __deepcopy__ of numpy.ndarray object at 0x7fc9b42346f0> returned a result with an error set

如果我将代码更改为下面的代码,它就会工作。但这是错误的正确解决方案吗

class Location(object):
    __pos_row = None  # col
    __pos_col = None  # row
    __neighbors = None
    is_wall = None
    _hash = None
    
    def __init__(self, row: int, col: int):
        self.__pos_row = row  # col
        self.__pos_col = col  # row
        self.__neighbors = None
        self.is_wall = None
        self._hash = None
我使用的是Python 3.8.6(和Numpy-Numpy==1.20.2)

  AttributeError: 'Location' object has no attribute '_hash'
class Location(object):
    __pos_row = None  # col
    __pos_col = None  # row
    __neighbors = None
    is_wall = None
    _hash = None
    
    def __init__(self, row: int, col: int):
        self.__pos_row = row  # col
        self.__pos_col = col  # row
        self.__neighbors = None
        self.is_wall = None
        self._hash = None