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