別のクラス Vertex を使用する Graph クラスを定義したモジュールがあります。
# Graph.py
class Graph(object):
def __init__(self):
self.vertList = {}
self.numVertices = 0
def addVertex(self,key):
self.numVertices += 1
newVert = Vertex(key)
self.vertList[key] = newVert
return newVert
def getVertex(self,k):
if k in self.vertList:
return self.vertList[k]
else:
return None
class Vertex(object):
def __init__(self,key):
self.id = key
self.connectedTo = {}
別のモジュールで使用するために Vertex クラスを拡張したい:
# BFSGraph.py
from Graph import Vertex,Graph
class Vertex(Vertex):
def __init__(self,key):
super(Vertex,self).__init__(key)
# extensions for BFS
self.predecessor = None
self.dist = 0
self.color = 'w' # white, grey, and black
class BFSGraph(Graph):
def getColor(self,k):
return self.getVertex(k).color
def test():
g=BFSGraph()
g.addVertex('a')
g.getColor('a')
テスト ルーチンを実行すると、「'Vertex' object has no attribute 'color'」が返されるため、Vertex に加えた変更は Graph に反映されず、BFSGraph は拡張 Vertex を使用していません。
Graph と BFSGraph に新しい Vertex を使用させるにはどうすればよいですか?