类对象没有属性Python

时间:2016-11-01 16:05:42

标签: python inheritance subclass

我有两个类,分为两个不同的文件。我试图从我的ArrayQueue类调用add_last(来自SinglyLinkedListClass)并最终得到'ArrayQueue'对象没有属性'_tail'。提前谢谢!

#--------------------------------------------------------------------
class EmptyList ( Exception ) :
    pass
# -------------------------------------------------------------------

class SinglyLinkedList :

    # ------------------------------------------------ #
    class _Node :                                      #
        def __init__ ( self, theElement, theNext ) :   #
            """Initializes newly created _Node"""      #
            self._element = theElement                 #
            self._next    = theNext                    #
    # ------------------------------------------------ #


    def __init__ ( self ) :
        """Initializes newly created SinglyLinkedList"""
        self._head = None
        self._tail = None
        self._size = 0
        self._lastNode = None

    def __len__ ( self ) :
        return self._size

    def __str__ ( self ) :
        """Returns string representation of SinglyLinkedList"""
        returnString = 'Head --> '
        current = self._head
        while current != None :
            returnString += ( str(current._element) + ' ' )
            current = current._next
        returnString += ( '<-- Tail ('
            + ('Empty' if self._size == 0 else 'Not Empty')
            + (' with ' + str(self._size) + ' elements)') )
        return returnString

    def __len__ ( self ) :
        """Returns length of SinglyLinkedList"""
        return self._size

    def is_empty ( self ) :
        """Returns True if SinglyLinkedList is empty"""
        return self._size == 0 

    def first ( self ) :
        """Returns element at head of SinglyLinkedList"""
        if self._size == 0 :
            raise EmptyList
        return self._head._element

    def last ( self ) :
        """Returns element at tail of SinglyLinkedList"""
        return self._tail._element

    def add_first ( self, newElement ) :
        """Inserts one element at head of SinglyLinkedL"""
        theNewNode = self._Node ( newElement, self._head )
        self._head = theNewNode
        if self._tail is None :
           self._tail = theNewNode
        self._size += 1

    def delete_first ( self ) : #deque
        """Deletes one element from head of SinglyLinkedList"""
        if self._head is None :
            raise EmptyList('Error: attempt delete_first on empty list!')
        deletedElement = self._head._element
        self._head = self._head._next
        if self._head is None :
            self._tail = None
        self._size -= 1
        return deletedElement

    def add_last ( self, newElement ) : #enque
        """Inserts one element at tail of SinglyLinkedList"""
        theNewNode = self._Node ( newElement, None )
        if self.is_empty() :
            self._head = theNewNode
            self._tail = theNewNode
        else:
            self._tail._next = theNewNode
            self._tail = theNewNode
        self._size += 1
# -------------------------------------------------------------------
class ArrayQueue (SinglyLinkedList):


    def __str__ ( self ) :
        returnString = 'Size is ' + str(self._size) + '  Front - > '
        for i in range ( self._front, self._front + self._size ) :
            j = i % len(self._data)
            returnString += ( str(self._data[j]) + ' ' )
        return ( returnString + ' <- Back' )


    def __len__ ( self ) :
        return self._size

    def __init__ ( self ) :
        self._data = SinglyLinkedList()
        self._size = 0
        self._front = 0

    def enqueue ( self, e ) :
        self._size += 1
        return SinglyLinkedList.add_last( self, e )

    def dequeue ( self ) :
        if self.is_empty( ) :
            raise Empty( 'Queue is empty!' )
        self._size -= 1
        return SinglyLinkedList.delete_first()

    def first ( self ) :
        if self.is_empty( ) :
            raise Empty( 'Queue is empty!' )
        return SinglyLinkedList.first()

    def is_empty ( self ) :
        return self._size == 0

if __name__ == '__main__' :
    mylist = ArrayQueue()
    choice = 0
    element = None 
    while choice < 4 :
        print ( '1.  enque' )
        print ( '2.  dequeue' )
        print ( '3.  first' )
        print ( '4.  is_empty' )
        choice = int(input('Enter Choice : '))
        if choice == 1 :
            element = int(input('Enter integer to add : '))
            mylist.enqueue(element)
        elif choice == 2 :
            try :
                print ( 'Deleted', mylist.dequeue() )
            except Empty as el :
                print ( el )

        elif choice == 3 :
            print( mylist.first())

        elif choice == 4 :
            mylist._size == 0

        print(mylist)

2 个答案:

答案 0 :(得分:0)

ArrayQueue没有尾部,因为你没有执行父类的 init ;

class ArrayQueue (SinglyLinkedList):

def __init__ ( self ) :
    super(ArrayQueue , self).__init__()  # call parent's init
    self._size = 0
    self._front = 0

答案 1 :(得分:0)

ArrayQueue定义了自己的__init__()方法,因此永远不会调用__init__()中继承的SinglyLinkedList(其中_tail已定义)。