无法弄清楚如何使用 Python 中的 class 创建 "list" 实例变量
Can't figure out how to create a "list" instance variable using class in Python
这是我的代码:
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": 衣服初始存款}', '{"amount": -20, "description": pants}', '{"amount": -200, "description": 转入衣服}', '{"amount" : 200, "description": 从食物中转移}']
这就是我要找的:
['{"amount": 1000, "description": 衣服初始定金}', '{"amount": -20, "description": pants}', '{"amount": -200, "description": 转移到服装}']
基本上我的 ledger 属性占用了所有我不想要的实例。
在 Python 中传递动态或可变(列表、字典)默认值时,最好将它们设置为 None 并指定它们函数内的预期值。
def __init__(self,category,balance=0,ledger=None):
If ledger is None:
ledger = []
self.category = category
self.balance = balance
self.ledger = ledger
否则 ledger 只会计算一次。参考:https://medium.com/python-features/how-to-avoid-classic-pitfall-while-passing-default-values-in-python-7002c0dc4c7c
这是我的代码:
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": 衣服初始存款}', '{"amount": -20, "description": pants}', '{"amount": -200, "description": 转入衣服}', '{"amount" : 200, "description": 从食物中转移}']
这就是我要找的:
['{"amount": 1000, "description": 衣服初始定金}', '{"amount": -20, "description": pants}', '{"amount": -200, "description": 转移到服装}']
基本上我的 ledger 属性占用了所有我不想要的实例。
在 Python 中传递动态或可变(列表、字典)默认值时,最好将它们设置为 None 并指定它们函数内的预期值。
def __init__(self,category,balance=0,ledger=None):
If ledger is None:
ledger = []
self.category = category
self.balance = balance
self.ledger = ledger
否则 ledger 只会计算一次。参考:https://medium.com/python-features/how-to-avoid-classic-pitfall-while-passing-default-values-in-python-7002c0dc4c7c