在__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?
最佳答案
原文链接:https://www.f2er.com/python/438431.html