我有一个来自其他人的 python 模块的片段,它引发了一个异常( AttributeError: 'NoneType' object has no attribute 'append' )。片段是:

def generateFragments(self):
    tr = self.deviceTransform()
    if tr is None:
        return
    pts = np.empty((2,len(self.data['x'])))
    pts[0] = self.data['x']
    pts[1] = self.data['y']
    pts = fn.transformCoordinates(tr, pts)
    self.fragments = []
    pts = np.clip(pts, -2**30, 2**30) ## prevent Qt segmentation fault.
                                      ## Still won't be able to render correctly, though.
    for i in xrange(len(self.data)):
        rec = self.data[i]
        pos = QtCore.QPointF(pts[0,i], pts[1,i])
        x,y,w,h = rec['fragCoords']
        rect = QtCore.QRectF(y, x, h, w)
        self.fragments.append(QtGui.QPainter.PixmapFragment.create(pos, rect))

异常消息是:
[14:35:59]  Ignored exception:

    |==============================>>
    |  Traceback (most recent call last):
    |    File "/usr/lib/pymodules/python2.7/pyqtgraph/debug.py", line 35, in w
    |      func(*args, **kwds)
    |    File "/usr/lib/pymodules/python2.7/pyqtgraph/graphicsItems/ScatterPlotItem.py", line 714, in paint
    |      self.generateFragments()
    |    File "/usr/lib/pymodules/python2.7/pyqtgraph/graphicsItems/ScatterPlotItem.py", line 685, in generateFragments
    |      self.fragments.append(QtGui.QPainter.PixmapFragment.create(pos, rect))
    |  AttributeError: 'NoneType' object has no attribute 'append'
    |
    |==============================<<

这不是“修复我的错误”的问题,而是“这怎么可能”的问题。上面几行,self.fragments 被声明为一个空列表。从那时起,self.fragments 的唯一修改是调用 append() 。类型怎么可能改变了?追加操作中是否存在可能将列表类型更改为 NoneType 的失败?

最佳答案

我想到的两种可能性是:

  • 并发(根据@ekhumoro 的评论)。
  • 类定义了一个 __getattribute__ 方法,该方法使通常的属性检索机制短路。
  • 关于python - 声明的列表引发 NoneType 异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26601053/

    10-12 19:29