2013-04-05 51 views
2

嘿傢伙,所以我得到這個「TypeError:'int'對象不可調用」錯誤當我運行此代碼時,我做了一些研究,我認爲這是與我的變量命名?有人可以向我解釋什麼是錯的?'int'對象是不可調用的錯誤python

class account(object): 
    def set(self, bal): 
     self.balance = bal 
    def balance(self): 
     return balance 

這是我的例子來看:

>>> a = account() 
>>> a.set(50) 
>>> a.balance() 
Traceback (most recent call last): 
    File "<pyshell#12>", line 1, in <module> 
    a.balance() 
TypeError: 'int' object is not callable 
+0

錯誤指向哪裏? – 2013-04-05 19:25:55

回答

13

你掩蓋你的方法balance用實例屬性balance。重命名一個或另一個。在實例王牌

def set(self, bal): 
    self._balance = bal 

def balance(self): 
    return self._balance 

這些屬性的類(除了數據描述符,認爲property S):你可以通過未決的預將其與例如下劃線命名實例屬性。從Class definitions documentation

Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with self.name = value . Both class and instance attributes are accessible through the notation 「 self.name 」, and an instance attribute hides a class attribute with the same name when accessed in this way.

+0

謝謝你的工作! – user12074577 2013-04-05 19:27:02

相關問題