声明的列表引发NoneType异常

时间:2014-10-28 04:45:46

标签: python list python-2.7 nonetype

我有一个来自别人的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'
    |  
    |==============================<<

这不是&#34;修复我的错误&#34;问题,但是&#34;这怎么可能&#34;题。上面几行,self.fragments被声明为空列表。从那时起,self.fragments的唯一修改就是调用append()。这种类型怎么可能改变?附加操作中是否可能出现故障,可能会将列表类型更改为NoneType

1 个答案:

答案 0 :(得分:0)

我想到的两种可能性是:

  • 并发(根据@ ekhumoro&#39;评论)。
  • 该类正在定义一个__getattribute__方法,该方法将通常的属性检索机制短路。