创建一个可选择的Python类

2024-05-08 04:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的应用程序创建一些自定义的Python类。当我试图调试我的代码时,我不能选择我的自定义类的实例,我收到错误“Object XXX is not picklable”。 我找到了这个页面https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled,但我不明白应该如何实现使我的类可选择的方法。 例如,您将如何修改以下类以便我可以选择它们的实例?在

class Point3D:
  def __init__ (self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

  def move(self, vector):
    self.x += vector.x
    self.y += vector.y
    self.z += vector.z
    return

  def isValidPoint(self):
    isNotValid = False
    isNotValid = math.isnan(self.x) or math.isnan(self.y) or math.isnan(self.z)
    return not isNotValid

以及

^{pr2}$

在这里您可以找到我尝试调试的代码:

from classDatabase import Point3D, PointCloud3D

testPoint1 = Point3D(1.5, 0.2, 2.3)
testPoint2 = Point3D(3.5, 1.2, 5.3)

testPointCloud3D = PointCloud3D()
testPointCloud3D.addPoint(testPoint1)
testPointCloud3D.addPoint(testPoint2)

最后是问题截图: enter image description here


Tags: or实例代码selfreturndefnotmath