Python的AttributeError的对象 [英] Python AttributeError Object
本文介绍了Python的AttributeError的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是什么做错了吗?这是我想完成我的家庭作业。我得到了奇怪的语法错误:
文件C:\\用户\\兰比尔\\桌面\\ myList2.py56行,在加
myList中。preV = myList.cursor
AttributeError的:MYLIST'对象有没有属性'$ P $光伏
我不明白我在做什么错了,code看起来很完美我。在preV是在ListNode定义,以及mkListNode。
类EmptyListNode():
空列表EmptyListNode的一个实例。
__slots__ =()类ListNode():
所有真正的价值包含四轮节点重新presented作为
ListNode的实例。
__slots__ =('数据','下一个','preV)类MYLIST():
对于链接轮类的容器。
它包含在车轮上的光标的参考。
不变:大小== 0当且仅当类型(光标)== EmptyListNode
__slots__ =('光标','大小')高清mkList():
使一个新的列表,大小为0,指着一个空节点
MYLIST = MYLIST()
mylist.cursor = EmptyListNode
mylist.size = 0
回报MYLIST高清mkListNode(数据,接下来,preV):
创建一个新的列表节点
返回一个新节点
listNode = ListNode()
listNode.data =数据
listNode.next =下一个
listNode。preV = preV
返回ListNode高清加(myList中,元素):
补充:在光标后节点添加到右侧车轮
效果:一个新的节点是在车轮刚在光标后
myList.size + = 1
如果myList.cursor == EmptyListNode:
myList.cursor = mkListNode(EmptyListNode,元件,EmptyListNode)
myList.cursor。preV = myList.cursor
myList.cursor.next = myList.cursor
其他:
TEMP = mkListNode(myList.cursor。preV,元素,myList.cursor.next)
myList.cursor。prev.next =气温
myList.cursor。preV =温度高清落后(列表):
前进:向前移动光标
效果:现在的光标是在车轮的下一个节点
myList.cursor = List.cursor。preV高清向前(myList中):
前进:向前移动光标
效果:现在的光标是在车轮的下一个节点
myList.cursor = myList.cursor.next高清GETNEXT(列表):
getNextElement:后光标位置在节点获取元素
效果:后光标位置的元件,则返回
返回List.cursor.next.dataDEF获得preV(列表):
getNextElement:后光标位置在节点获取元素
效果:后光标位置的元件,则返回
返回List.cursor。prev.data高清的printList(myList中):
打印在车轮上的所有项目
字符串1 =
电流= myList.cursor
因为我在范围内(mylist.size):
字符串1 + = current.data
电流= current.next高清尺寸(myList中):
大小:轮 - > numl
回报mylist.sizeDEF删除(myList中):
删除:在光标从车轮删除节点
效果:滚轮后的节点被删除
precondition:轮毂尺寸> 0
如果大小(myList中)== 1:
myList.cursor = EmptyListNode()
其他:
myList.cursor.next。preV = List.cursor。preV
myList.cursor。prev.next = List.cursor.next
myList.cursor.data = List.cursor。prev.data
myList.cursor = List.cursor。preV
myList.cursor。prev.next = List.cursor
myList.size - = 1
任何帮助吗?谢谢
解决方案
myList中。preV = myList.cursor
myList中
显然键入 MYLIST
,它并没有定义 $ P $光伏
作为插槽。也许你的意思是这样呢?
myList.cursor。preV = myList.cursor
myList.cursor.next = myList.cursor
What am I doing wrong? This is my homework assignment I am trying to finish. I got the weird syntax error:
File "C:\Users\Ranbir\Desktop\myList2.py", line 56, in add
myList.prev = myList.cursor
AttributeError: 'MyList' object has no attribute 'prev'
I don't see what I am doing wrong, the code looks perfect to me. The prev is defined in the ListNode and as well as mkListNode.
class EmptyListNode():
"""
An empty list is an instance of EmptyListNode.
"""
__slots__ = ()
class ListNode():
"""
All true value-containing wheel nodes are represented as
instances of ListNode.
"""
__slots__ = ('data', 'next', 'prev')
class MyList():
"""
The container for a linked wheel class.
It contains a reference to a cursor in the wheel.
Invariant: size==0 iff type(cursor)==EmptyListNode
"""
__slots__ = ('cursor', 'size')
def mkList():
"""
Makes a new list, with size 0, pointing to a Empty Node
"""
mylist = MyList()
mylist.cursor = EmptyListNode
mylist.size = 0
return mylist
def mkListNode(data, next, prev):
"""
Make a new list node
Returns a new node
"""
listNode = ListNode()
listNode.data = data
listNode.next = next
listNode.prev = prev
return ListNode
def add(myList, element):
"""
add: Add a node to the wheel right after the cursor
Effect: A new node is in the wheel just after the cursor
"""
myList.size += 1
if myList.cursor == EmptyListNode:
myList.cursor = mkListNode(EmptyListNode, element, EmptyListNode)
myList.cursor.prev = myList.cursor
myList.cursor.next = myList.cursor
else:
temp = mkListNode(myList.cursor.prev, element, myList.cursor.next)
myList.cursor.prev.next = temp
myList.cursor.prev = temp
def backward(List):
"""
advance: Advance the cursor
Effect: The cursor is now at the next node in the wheel
"""
myList.cursor = List.cursor.prev
def forward(myList):
"""
advance: Advance the cursor
Effect: The cursor is now at the next node in the wheel
"""
myList.cursor = myList.cursor.next
def getNext(List):
"""
getNextElement: Get element at node after cursor position
Effect: The element after cursor position is returned
"""
return List.cursor.next.data
def getPrev(List):
"""
getNextElement: Get element at node after cursor position
Effect: The element after cursor position is returned
"""
return List.cursor.prev.data
def printList(myList):
"""
Prints the all the items in the wheel
"""
string1 = ""
current = myList.cursor
for i in range(mylist.size):
string1 += current.data
current = current.next
def size(myList):
"""
size: wheel -> numl
"""
return mylist.size
def remove(myList):
"""
remove: Remove the node at the cursor from the wheel
Effect: The node after the wheel is removed
Precondition: wheel size > 0
"""
if size(myList) == 1:
myList.cursor = EmptyListNode()
else:
myList.cursor.next.prev= List.cursor.prev
myList.cursor.prev.next = List.cursor.next
myList.cursor.data = List.cursor.prev.data
myList.cursor = List.cursor.prev
myList.cursor.prev.next = List.cursor
myList.size -= 1
Any help? Thanks
解决方案
myList.prev = myList.cursor
myList
is apparently of type MyList
, which does not define prev
as a slot. Perhaps you meant this instead?
myList.cursor.prev = myList.cursor
myList.cursor.next = myList.cursor
这篇关于Python的AttributeError的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文