2013-04-05 2 views
2

이 코드를 실행할 때이 "TypeError : 'int'객체가 호출 가능하지 않습니다. '오류가 발생했습니다. 일부 연구를 수행했는데 변수 이름이 뭔가있는 것 같아요. ? 누군가 나에게 무엇이 잘못되었는지 설명해 주시겠습니까? 당신은 예와 방법 balance 마스킹하는'int'객체가 호출 가능하지 않음 오류 파이썬

>>> 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

정확히 오류가 가리키는 경우 : Class definitions documentation에서? –

답변

13

balance 속성 :

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

은 여기 내 예를 실행합니다. 둘 중 하나의 이름을 바꿉니다. (; property의 생각 데이터 설명 제외) 인스턴스 트럼프의 클래스들을

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

def balance(self): 
    return self._balance 

속성 : 당신은 예를 들어 밑줄을 사전에 출원하여 인스턴스 속성의 이름을 바꿀 수 있습니다.

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

관련 문제