类 和实例
Classes and Instances
我是 OOP 的新手,并且通过编写预算来练习 class 这样:
class Budget:
category_record = []
def __init__(self, category = '', balance = 0):
self.category = category
self.balance = balance
Budget.category_record.append(self)
def deposit(self, amount):
self.balance += amount
print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
return
def withdraw(self, amount):
if amount > self.balance:
print('Insufficient Balance, unable to withdraw')
else:
self.balance -= amount
print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
return
def category_balance(self):
print(f'Your balance is {self.balance} for your {self.category} budget')
我试图记录所有预算实例class作为一种方法(如果我的条款有误,请原谅我,还在习惯它们)
# Instantiate Food budget
food = Budget('food')
# deposit 200 into food budget
food.deposit(200)
# withdraw 100 from food budget
food.withdraw(100)
#instantaite rent budget
rent = Budget('house', 5000)
# check balance of rent budget
rent.category_balance()
这样当我在预算 class 上调用记录方法时,我可以获得 ['food'、'rent'] 的列表
或者如果可能的话,一个字典,其中键作为类别,值作为余额 {'food':100...}
如果您想获得预算的所有实例的列表 class,您的方法可以 return 您已经在其中存储所有内容的 category_record
列表创建实例时。但是,您似乎希望列表包含要为其分配实例的变量的名称(标识符)。预算 class 将无法访问这些名称,因此,我建议您尝试使用类别名称,因为这些名称将记录在您的 category_record
变量中。
根据您的问题,我假设您希望调用该方法 record
。最好的方法是使用带有内置 classmethod
装饰器的 class 方法。这些方法作用于 class 本身,而不是实例,因此采用 cls
参数而不是 self
参数(这两个名称只是约定俗成,但我强烈建议反对偏离惯例,因为这会使您的代码对其他人来说不必要地复杂)。例如:
class Budget:
...
# other code
....
category_record = []
@classmethod
def record(cls):
return [budget.category for budget in cls.category_record]
# If you are unfamiliar with this syntax, search up "list comprehensions"
# Access the list
print(Budget.category_record)
如果你想要一个字典,你可以用这个替换那个方法:
class Budget:
...
# other code
...
category_record = []
@classmethod
def record(cls):
return {budget.category:budget.balance for budget in cls.category_record}
# If you are unfamiliar with this syntax, search up "dictionary comprehensions"
# Access the dictionary
print(Budget.category_record)
详细说明我的@classmethod 评论:
class Budget:
category_record = []
def __init__(self, category = '', balance = 0):
self.category = category
self.balance = balance
Budget.category_record.append(self)
def deposit(self, amount):
self.balance += amount
print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
return
def withdraw(self, amount):
if amount > self.balance:
print('Insufficient Balance, unable to withdraw')
else:
self.balance -= amount
print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
return
def category_balance(self):
print(f'Your balance is {self.balance} for your {self.category} budget')
@classmethod
def budget_list(cls):
if len(Budget.category_record)== 0:
print("No budgets created")
else:
print("Budget Categories:")
for budge in Budget.category_record:
print(budge.category)
@classmethod
def full_report(cls):
if len(Budget.category_record)== 0:
print("No budgets created")
else:
print("Budget Balances:")
for budge in Budget.category_record:
print(f"{budge.category} : {budge.balance}")
# Instantiate Food budget
food = Budget('food')
# deposit 200 into food budget
food.deposit(200)
# withdraw 100 from food budget
food.withdraw(100)
#instantaite rent budget
rent = Budget('house', 5000)
# check balance of rent budget
rent.category_balance()
Budget.budget_list()
Budget.full_report()
我是 OOP 的新手,并且通过编写预算来练习 class 这样:
class Budget:
category_record = []
def __init__(self, category = '', balance = 0):
self.category = category
self.balance = balance
Budget.category_record.append(self)
def deposit(self, amount):
self.balance += amount
print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
return
def withdraw(self, amount):
if amount > self.balance:
print('Insufficient Balance, unable to withdraw')
else:
self.balance -= amount
print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
return
def category_balance(self):
print(f'Your balance is {self.balance} for your {self.category} budget')
我试图记录所有预算实例class作为一种方法(如果我的条款有误,请原谅我,还在习惯它们)
# Instantiate Food budget
food = Budget('food')
# deposit 200 into food budget
food.deposit(200)
# withdraw 100 from food budget
food.withdraw(100)
#instantaite rent budget
rent = Budget('house', 5000)
# check balance of rent budget
rent.category_balance()
这样当我在预算 class 上调用记录方法时,我可以获得 ['food'、'rent'] 的列表 或者如果可能的话,一个字典,其中键作为类别,值作为余额 {'food':100...}
如果您想获得预算的所有实例的列表 class,您的方法可以 return 您已经在其中存储所有内容的 category_record
列表创建实例时。但是,您似乎希望列表包含要为其分配实例的变量的名称(标识符)。预算 class 将无法访问这些名称,因此,我建议您尝试使用类别名称,因为这些名称将记录在您的 category_record
变量中。
根据您的问题,我假设您希望调用该方法 record
。最好的方法是使用带有内置 classmethod
装饰器的 class 方法。这些方法作用于 class 本身,而不是实例,因此采用 cls
参数而不是 self
参数(这两个名称只是约定俗成,但我强烈建议反对偏离惯例,因为这会使您的代码对其他人来说不必要地复杂)。例如:
class Budget:
...
# other code
....
category_record = []
@classmethod
def record(cls):
return [budget.category for budget in cls.category_record]
# If you are unfamiliar with this syntax, search up "list comprehensions"
# Access the list
print(Budget.category_record)
如果你想要一个字典,你可以用这个替换那个方法:
class Budget:
...
# other code
...
category_record = []
@classmethod
def record(cls):
return {budget.category:budget.balance for budget in cls.category_record}
# If you are unfamiliar with this syntax, search up "dictionary comprehensions"
# Access the dictionary
print(Budget.category_record)
详细说明我的@classmethod 评论:
class Budget:
category_record = []
def __init__(self, category = '', balance = 0):
self.category = category
self.balance = balance
Budget.category_record.append(self)
def deposit(self, amount):
self.balance += amount
print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
return
def withdraw(self, amount):
if amount > self.balance:
print('Insufficient Balance, unable to withdraw')
else:
self.balance -= amount
print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
return
def category_balance(self):
print(f'Your balance is {self.balance} for your {self.category} budget')
@classmethod
def budget_list(cls):
if len(Budget.category_record)== 0:
print("No budgets created")
else:
print("Budget Categories:")
for budge in Budget.category_record:
print(budge.category)
@classmethod
def full_report(cls):
if len(Budget.category_record)== 0:
print("No budgets created")
else:
print("Budget Balances:")
for budge in Budget.category_record:
print(f"{budge.category} : {budge.balance}")
# Instantiate Food budget
food = Budget('food')
# deposit 200 into food budget
food.deposit(200)
# withdraw 100 from food budget
food.withdraw(100)
#instantaite rent budget
rent = Budget('house', 5000)
# check balance of rent budget
rent.category_balance()
Budget.budget_list()
Budget.full_report()