这是我的代码:
class Category():
def __init__(self,category,balance=0,ledger=[]):
self.category = category
self.balance = balance
self.ledger = ledger
def check_funds(self,amt):
if amt > self.balance:
return False
else:
return True
def deposit(self, dep_amt, description=""):
self.balance +=dep_amt
dep_note = "{\"amount\": "+str(dep_amt)+", \"description\": "+description+"}"
self.ledger.append(dep_note)
def withdraw(self, wd_amt, description=""):
if not self.check_funds(wd_amt):
return False
else:
self.balance -= wd_amt
wd_note = "{\"amount\": "+"-"+str(wd_amt)+", \"description\": "+description+"}"
self.ledger.append(wd_note)
return True
def get_balance(self):
return self.balance
def transfer(self,tf_amt,category):
wd_description = "Transfer to "+category.category
dp_description = "Transfer from "+self.category
self.withdraw(tf_amt,wd_description)
category.deposit(tf_amt,dp_description)
创建实例:
food = Category('food')
food.deposit(1000,"food initial deposit")
food.withdraw(300,"groceries")
clothing = Category('clothing')
clothing.deposit(1000,"clothes initial deposit")
clothing.withdraw(20,"pants")
food.transfer(200,clothing)
运行代码时,clothing.ledger
这是输出:
['{"amount": 1000, "description": 食品初始押金}', '{"amount": -300, "description": 杂货}', '{"amount": 1000, "description": 衣服初始deposit}', '{"amount": -20, "description": 裤子}', '{"amount": -200, "description": 转入衣物}', '{"amount": 200, "description ":从食物转移}']
这就是我要找的:
['{"amount": 1000, "description": 衣服初始存款}', '{"amount": -20, "description": 裤子}', '{"amount": -200, "description": 转账去衣服}']
基本上我的分类帐属性采用所有我不想要的实例。