在 __init__.py 文件中定义一个类是否被认为是不好的实践?我有一个类似于税收计算器的情况。我想要一个计算器类,然后是一组州特定的税收计算器类,计算器类内部引用这些类。类似于:
class TaxCalculator():
def __init__(self):
self.ca_tax_calculator = CATaxCalculator()
self.ny_tax_calculator = NYTaxCalculator()
def get_tax_calculator_for_state(self, state):
tax_calculator = None
if state == "CA":
tax_calculator = self.ca_tax_calculator
else:
tax_calculator = self.ny_tax_calculator
return tax_calculator
def calculate(self, purchase_info):
return self.get_tax_calculator_for_state(purchase_info.state).calculate(purchase_info.amount)
我考虑的目录结构如下所示:
/calculators/__init__.py
/calculators/ca.py
/calculators/ny.py
__init__.py中包含了"TaxCalculator"函数。
类会引用计算器,例如:
from calculators import TaxCalculator
calculator = TaxCalculator().calculate(purchase_info)
这被认为是不好的实践,或者说不太符合Pythonic风格吗?