這個例子是由John V. Guttag使用Python編寫的介紹計算&編程屬性錯誤,同時創造一流
class IntSet(object):
def _init_(self):
self.vals= []
#Rest of the code is fine
def insert(self,x):
if not x in self.vals:
self.vals.append(x)
s= IntSet()
s.insert(3)
我得到一個錯誤:
Traceback (most recent call last):
File "/Users/abhimanyuaryan/Python/Classes/main.py", line 43, in <module>
s.insert(3)
File "/Users/abhimanyuaryan/Python/Classes/main.py", line 13, in insert
if not e in self.vals:
AttributeError: 'IntSet' object has no attribute 'vals'
oops謝謝你的幫助......你還可以告訴爲什麼對象在IntSet類中作爲參數傳遞嗎? – 2014-11-02 15:57:35
@ codejam.tk這不是一個參數,這是一個超類。你有沒有試過閱讀https://docs.python.org/2/tutorial/classes.html或類似的? – jonrsharpe 2014-11-02 15:59:09