[英]AttributeError: 'dict' object has no attribute 'Instance' python
[英]Python 2.7. Reading class instance property leads to error of type AttributeError: 'dict' object has no attribute. Why?
嘗試使用python代碼包裝boost庫時遇到了一個問題。 我有以下與Boost庫周圍的Python包裝器關聯的類。
class Edge2:x1 = -1 y1 = -1 x2 = -1 y2 = -1 is_primary = False site = -1 is_linear = False
def __init__(self, x1,y1,x2,y2, is_primary, site, is_linear):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.is_primary = is_primary
self.site = site
self.is_linear = is_linear
類VoronoiCell:cellId = -1 segments = None
def __init__(self,cellId):
self.cellId = cellId
self.segments = []
當我遍歷C ++結果並將所需的信息轉換為Voronoi單元格類型的python對象時。 段列表是Edge2類型的元素的集合。
if(c_edges2[i-1].site == edge2.site):
print "Append segment to cell"
self.outputCells[-1].segments.append(edge2)
else:
print "Append segment to cell"
cell = VoronoiCell(edge2.site)
cell.segments.append(Edge2(x1,y1,x2,y2,edge2.isPrimary, edge2.site, edge2.isLinear))
self.outputCells.append(cell)
但是,當我嘗試通過外部python遍歷結果時,當我期望使用類時,我的句段被標記為字典。 而且它們甚至都不是合適的字典,因為我什至不能使用字典語法來訪問類成員。
AttributeError:'dict'對象沒有屬性'x1'
cells = pv.GetCellSegments()
print "Cell Count: " + str(len(cells))
for c in cells:
print "Cell identifier: {0}".format(c.cellId)#Works fine
for s in c.segments:
print " " + str(s.x1)#Fails with error AttributeError: 'dict' object has no attribute 'x1'
我有點不解。 有誰知道我為什么收到此錯誤消息?
謝謝斯文!
不是確切的答案,但您使我走上了正確的道路……您已經接近了! 我忘了有時讓他們呆一會兒,這是多么簡單的問題。
當我做self.outputCells [-1] .segments.append(edge2)時,我實際上是在添加映射的C結構,而不是python類。 我應該像在病房之后一樣打電話給我的構造函數
cell.segments.append(Edge2(x1,y1,x2,y2,edge2.isPrimary, edge2.site, edge2.isLinear))
再次感謝Sven的評論!
法比恩
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.