类对象没有Python属性
我有两个类,在两个不同的文件中。我试图从我的ArrayQueue类调用add_last(从SingleyLinkedListClass调用),结果是“ArrayQueue”对象没有属性“_tail”。提前谢谢类对象没有Python属性,python,inheritance,subclass,Python,Inheritance,Subclass,我有两个类,在两个不同的文件中。我试图从我的ArrayQueue类调用add_last(从SingleyLinkedListClass调用),结果是“ArrayQueue”对象没有属性“_tail”。提前谢谢 #-------------------------------------------------------------------- class EmptyList ( Exception ) : pass # --------------------------------
#--------------------------------------------------------------------
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)
#--------------------------------------------------------------------
类EmptyList(异常):
通过
# -------------------------------------------------------------------
类SingleyLinkedList:
# ------------------------------------------------ #
类_节点:#
定义初始(自、元素、下一个):#
“”“初始化新创建的\u节点”“”#
self.\u元素=元素#
self.\u next=下一个#
# ------------------------------------------------ #
定义初始化(自):
“”“初始化新创建的SingleLinkedList”“”
自身。_头=无
self.\u tail=无
自身大小=0
self.\u lastNode=None
定义(自我):
返回自我。\u大小
定义(自我):
“”“返回SingleLinkedList的字符串表示形式”“”
returnString='Head-->'
电流=自身压头
当前时!=无:
returnString+=(str(当前元素)+'')
当前=当前。\ u下一步
returnString+=(“”
对于范围内的i(自前、自前+自大小):
j=i%len(自身数据)
returnString+=(str(self.\u数据[j])+'')
return(returnString+”ArrayQueue没有尾部,因为您没有执行父类的init
class ArrayQueue (SinglyLinkedList):
def __init__ ( self ) :
super(ArrayQueue , self).__init__() # call parent's init
self._size = 0
self._front = 0
ArrayQueue
定义了它自己的\uuuu init\uuuu()
方法,因此继承自SingleyLinkedList
的\uuu init\uuuuu()
(定义了\u tail
)从未调用。为什么\u tail
?为什么\u
?似乎没有必要。为什么您要首先实现自己的单链接列表…?单链接列表类中有一个称为节点的类。\ u tail引用链接列表的结尾。这是学校的项目,我将在用于从用户指定的文件中读取和执行数学运算的单链表形式。我似乎不明白为什么我不能将单链表中的add_last方法继承到ArrayQueue的enqueue方法中。成员名称下划线的使用在“感谢您的回复”中进行了描述!感谢您的回复!