2014-11-05 51 views
1

我們剛收到這段代碼,我不明白_todo = [ self ]是什麼意思。變量= [self]是什麼意思?

class ABR(object): 
    def __init__(self,ordre,root=None): 
     self.__root = None 
     assert ordre in (lt,gt,leq,geq) 
     self.__order = ordre 
     if isinstance(root,Sommet): 
      self.__root = root 

    def __str__(self): 
     """ effectue un parcours infixe """ 
     _todo = [ self ] 
     _done = "[ " 
     while _todo != [] : 
      _next = _todo.pop(0) 
      if _next != None : 
       if isinstance(_next,Sommet): 
        _done += "%s, " % _next.val 
       elif not _next.estVide: 
        if _next.hasRightSubTree : 
         _todo.insert(0,_next.rightSubTree) 
        _todo.insert(0,_next.racine) 
        if _next.hasLeftSubTree : 
         _todo.insert(0,_next.leftSubTree) 
     return _done[:-2]+" ]" 
+0

'self'指的是ABR類的一個實例。 – Brandon 2014-11-05 14:54:06

+0

'_todo = [self]'是一個局部變量列表對象,其中包含'class instance'。 – 2014-11-05 14:56:31

+1

'_todo'是一個局部變量,它使用'ABR'類的當前實例(對象)作爲列表的第一個元素進行初始化。它看起來像是抽象二叉樹的一個遍歷,返回二叉樹的最後2個元素。 – karthikr 2014-11-05 14:56:51

回答

0

_todo = [ self ]創建與作爲在其上__str__方法被調用的對象的單個元素命名_todo列表。 (該方法__str__通常由print聲明調用。)

即變量隨後被用於前面加上/追加孩子的字符串表示來創建樹的扁平化表示。