2014-01-09 129 views
0

我是新來的蟒蛇,並有一些問題,傳遞對象參數的多個inheritens傳遞參數 我有類圖與蟒蛇

class Graph: 
    def __init__(self,Name): 
     self.Name = Name 
     self.Nodes = {} 
     self.Edges = {} 
    def addEdgeAndNodes(self,sourceName,targetName): ''' code''' 
    def getEdgesCount(self):'''code ''' 
    def getNodesCount(self):''' code ''' 
#some more code 

和其他文件UpdateGraph類文件

import Graph 
class UpdateGraph(Graph.Graph): 
     def __init__(self,Name): 
      super().__init__(Name) 
     def addUpdateEdgeAndNodes(self,sourceName,targetName,sourceType,targetType,edgeType): 
#some more code 

和GraphBuilder文件中:

from Graph import Graph 
from UpdateGraph import UpdateGraph 
class GraphDFS: 
    def __init__(self,graph): 
     self.Graph = graph 
     self.dfsRes = {} 

    def dfs(self): 
     #some code 
     print("-->"+self.Graph.getNodesCount()) 

#some code 

class GraphBuilder: 
     def __init__(self): 
      self.Build() 

    def Build(self): 
      self.Graph = UpdateGraph(self.name) 

def main(): 
    Graph = GraphBuilder(name) 
    dfs = GraphDFS(Graph) 
    dfs.dfs() 

main() 

當我是試圖運行它引發錯誤的代碼: AttributeError的: 'GraphBuilder' 對象沒有屬性 'getNodesCount' 在行 打印( 「 - >」 + self.Graph.getNodesCount())

圖是updateGraph鍵入 我怎樣才能通過這個varible,它會通過圖形生成器功能來識別

+1

顯然,'graph'是'GraphBuilder',不'UpdateGraph'對象,所以兩個問題的一個實例:什麼是'GraphBuilder' ,以及如何設置'graph'的值? – chepner

+0

Graph Builder剛剛添加文本文件中的數據並生成圖形 – dima

+0

顯示您構建'graph'對象的代碼 – qurban

回答

0
def main(): 
    Graph = GraphBuilder(name) 
    dfs = GraphDFS(Graph) 
    dfs.dfs() 

你逝去的GraphBuilder的對象GraphDFSself.GraphGraphBuilder一個實例,你這個對象調用getNodesCount,這對象不包含這樣的方法。

from Graph import Graph 
from UpdateGraph import UpdateGraph 
class GraphDFS: 
    def __init__(self,graph): 
     self.Graph = graph 
     self.dfsRes = {} 

    def dfs(self): 
     #some code 
     print("-->"+self.Graph.getNodesCount()) 

#some code 

class GraphBuilder: 
    def __init__(self, name): 
     self.name = name 

    def Build(self): 
     self.Graph = UpdateGraph(self.name) 

def main(): 
    name = "" # define the name variable 
    Graph = GraphBuilder(name) 
    Graph.Build() 
    dfs = GraphDFS(Graph.Graph) 
    dfs.dfs() 

main() 

把上面的代碼中GrapthBuilder文件,這應該做工精細