![](/img/trans.png)
[英]Python - AttributeError: 'Particle' object has no attribute 'display'
[英]AttributeError: 'list' object has no attribute 'display' in python
當我使用類對象調用顯示函數時出現錯誤,我該怎么做才能克服這個問題?
class A:
def __init__(self, fname, lname, age):
self.fname = fname
self.lname = lname
self.age = age
def disply(self):
fp = open("abc","r")
for lines in fp:
temp = lines.split(", ")[-1]
fp.close()
print a
a = [A("Taylor","Launter",22), A("James","bond",40)]
a.display()
您有一個名為實例列表a
。 a
是不是實例/ S,這是一個列表。
您可能打算這樣做:
for myobject in a:
myobject.disply() # Note "disply" instead of "display"
a = [A("Taylor","Launter",22), A("James","bond",40)]
a.display()
現在a
是列表。 python中的列表沒有display
方法。
您可能真正想做的是調用A
對象的display
方法。 如果是這種情況,您可能想做這樣的事情
for currentObject in [A("Taylor","Launter",22), A("James","bond",40)]:
currentObject.display()
編輯您的display
方法對我沒有任何意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.