2016-09-17 8 views
-2
class Account(object): 
    def __init__(self,holder, number, balance, credit_line = 1500): 
     self.holder = holder 
     self.number = number 
     self.balance = balance 
     self.credit_line = credit_line 

    def deposit(self, amount): 
     self.balance = amount 
    def withdraw(self, amount): 
     if(self.balance - amount < -self.credit_line): 
      return False 
     else: 
      self.balance -= amount 
      return True 

    def balance(self): 
     return self.balance 
    def holder(self): 
     return self.holder 

    def transfer(self, target, amount): 
     if(self.balance - amount < -self.credit_line): 
      #coverage insufficient 
      return False 
     else: 
      self.balance -= amount 
      target.balance += amount 
      return True 
Guido = Account("Guido", 10 ,1000.50) 
Guido.balance() 
------------------------------------------------------------------------- 


Traceback (most recent call last): 
    File "Account.py", line 31, in <module> 
    Guido.balance() 
TypeError: 'float' object is not callable 
+0

'balance.functions = amount'가'balance' 함수를 덮어 쓴다. –

+0

다른 답변으로 문제가 설명되었지만'balance'와'holder' 메소드와 같은 "접근 자"는 일반적으로 파이썬에서는 필요 없다. 외부 코드에서 특성에 액세스 할 수 있습니다 (이 경우 문제와 연결된 이름이 같은 경우). 나중에 값에 대한 특수 처리 (예 : 소수점 이하 자리 수를 반올림)를 수행해야하는 경우 '속성'을 사용하여 속성 조회를 함수 호출로 바꿀 수 있습니다. – Blckknght

답변

1

__init__ 메서드에서 메서드 balance을 재정의했습니다. 필드 이름을 _balance으로 변경하거나 balance 메소드를 제거하고 Guido.balance을 사용할 수 있습니다.

또한 ............... 곧 당신에게 소문자 (즉 guido = Account(...)하지 Guido)

0
class Account(object): 
    def __init__(self,holder, number, balance, credit_line = 1500): 
     self.holder = holder 
     self.number = number 
     self.balance = balance 
     self.credit_line = credit_line 

    def deposit(self, amount): 
     self.balance = amount 
    def withdraw(self, amount): 
     if amount > self.balance: 
      print "Amount greater than available balance" 

     else: 
      self.balance -= amount 
      return True 

    def bala_nce(self): 
     return self.balance 
    def hold_er(self): 
     return self.holder 
    def num(self): 

     return self.number 


    def transfer(self, target, amount): 
     if(self.balance - amount < -self.credit_line): 
      #coverage insufficient 
      return False 
     else: 
      self.balance -= amount 
      target.balance += amount 
      return True 

guido = Account("Guido", 10 ,10000.100) 
guido.withdraw(2300.100) 

print "Account name: " ,guido.hold_er() 
print "available balance: $",guido.bala_nce() 

고맙습니다부터 시작하여 변수 이름을 지정해야한다는주의 그것의 작업은

관련 문제