如何使用多重继承不混淆 class 变量?
How to not mix up class variables using multiple inheritance?
我需要创建一个 'Balance' class,这将是两个 parent classes 'Income' 和 'Expenses' 的儿子。 'Income' 和 'Expenses' 应该是容器,我将在其中放置一些数字,稍后找出 'Income' 的总和与 'Expenses' 的总和之间的差异。
from dataclasses import dataclass
@dataclass
class Income:
first_Income: int = 0
second_Income: int = 0
third_Income: int = 0
def all_vars_Income(self):
return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith('__')]
def sum_Income(self):
return sum([getattr(self, var) for var in self.all_vars_Income()])
@dataclass
class Expenses:
first_Expenses: int = 0
second_Expenses: int = 0
third_Expenses: int = 0
def all_vars_Expenses(self):
return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith('__')]
def sum_Expenses(self):
return sum([getattr(self, var) for var in self.all_vars_Expenses()])
class Balance(Income, Expenses):
def sum_Balance(self):
return Income.sum_Income(self) - Expenses.sum_Expenses(self)
user = Balance()
print(Balance.all_vars_Income(user))
# ['first_Expenses', 'first_Income', 'second_Expenses', 'second_Income', 'third_Expenses', 'third_Income']
print(Income.all_vars_Income(user))
# ['first_Expenses', 'first_Income', 'second_Expenses', 'second_Income', 'third_Expenses', 'third_Income']
print(Income.all_vars_Income(Income))
# ['first_Income', 'second_Income', 'third_Income']
user.__setattr__('first_Income', 50)
user.__setattr__('second_Expenses', 100)
print(Income.sum_Income(user))
# 150
print(user.sum_Balance())
# 0
因此,如您所见,问题是第一个 class 的变量和第二个 class 的变量有点混淆。我想将变量保持在适当的 classes 范围内,并且只使用这些变量的总和来计算差异。
当我在这一行 "user.__setattr__('first_Income', 50)"
时,我只想更改收入 class 的值,这样我就可以将支出 class 的所有变量的总和保持为 0。
可能,它是混乱的,但我希望你明白这一点。
我建议用这种方式来构建你的代码,它不仅解决了你的主要问题,而且符合 python 的风格指南,更好地利用了 类 的能力和功能(尽管为简单起见,我省略了异常处理):
from dataclasses import dataclass, field
@dataclass
class Income:
incomes: list[int] = field(default_factory=list)
def sum_incomes(self):
return sum(self.incomes)
def add_income(self, income):
self.incomes.append(income)
def modify_income(self, number, income):
self.incomes[number] = income
@dataclass
class Expenses:
expenses: list[int] = field(default_factory=list)
def sum_expenses(self):
return sum(self.expenses)
def add_expense(self, expense):
self.expenses.append(expense)
def modify_expense(self, number, expense):
self.expenses[number] = expense
@dataclass
class Balance(Income, Expenses):
def sum_balance(self):
return self.sum_incomes() - self.sum_expenses()
user = Balance()
user.add_income(50)
user.add_expense(100)
print(user.sum_incomes())
# 50
print(user.sum_balance())
# -50
user.modify_income(0, 200)
print(user.sum_balance())
# 100
我需要创建一个 'Balance' class,这将是两个 parent classes 'Income' 和 'Expenses' 的儿子。 'Income' 和 'Expenses' 应该是容器,我将在其中放置一些数字,稍后找出 'Income' 的总和与 'Expenses' 的总和之间的差异。
from dataclasses import dataclass
@dataclass
class Income:
first_Income: int = 0
second_Income: int = 0
third_Income: int = 0
def all_vars_Income(self):
return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith('__')]
def sum_Income(self):
return sum([getattr(self, var) for var in self.all_vars_Income()])
@dataclass
class Expenses:
first_Expenses: int = 0
second_Expenses: int = 0
third_Expenses: int = 0
def all_vars_Expenses(self):
return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith('__')]
def sum_Expenses(self):
return sum([getattr(self, var) for var in self.all_vars_Expenses()])
class Balance(Income, Expenses):
def sum_Balance(self):
return Income.sum_Income(self) - Expenses.sum_Expenses(self)
user = Balance()
print(Balance.all_vars_Income(user))
# ['first_Expenses', 'first_Income', 'second_Expenses', 'second_Income', 'third_Expenses', 'third_Income']
print(Income.all_vars_Income(user))
# ['first_Expenses', 'first_Income', 'second_Expenses', 'second_Income', 'third_Expenses', 'third_Income']
print(Income.all_vars_Income(Income))
# ['first_Income', 'second_Income', 'third_Income']
user.__setattr__('first_Income', 50)
user.__setattr__('second_Expenses', 100)
print(Income.sum_Income(user))
# 150
print(user.sum_Balance())
# 0
因此,如您所见,问题是第一个 class 的变量和第二个 class 的变量有点混淆。我想将变量保持在适当的 classes 范围内,并且只使用这些变量的总和来计算差异。
当我在这一行 "user.__setattr__('first_Income', 50)"
时,我只想更改收入 class 的值,这样我就可以将支出 class 的所有变量的总和保持为 0。
可能,它是混乱的,但我希望你明白这一点。
我建议用这种方式来构建你的代码,它不仅解决了你的主要问题,而且符合 python 的风格指南,更好地利用了 类 的能力和功能(尽管为简单起见,我省略了异常处理):
from dataclasses import dataclass, field
@dataclass
class Income:
incomes: list[int] = field(default_factory=list)
def sum_incomes(self):
return sum(self.incomes)
def add_income(self, income):
self.incomes.append(income)
def modify_income(self, number, income):
self.incomes[number] = income
@dataclass
class Expenses:
expenses: list[int] = field(default_factory=list)
def sum_expenses(self):
return sum(self.expenses)
def add_expense(self, expense):
self.expenses.append(expense)
def modify_expense(self, number, expense):
self.expenses[number] = expense
@dataclass
class Balance(Income, Expenses):
def sum_balance(self):
return self.sum_incomes() - self.sum_expenses()
user = Balance()
user.add_income(50)
user.add_expense(100)
print(user.sum_incomes())
# 50
print(user.sum_balance())
# -50
user.modify_income(0, 200)
print(user.sum_balance())
# 100