Is it considered bad practice to have a class defined in a __init__.py file? I have a case that is similar to a tax calculator. I want to have a calculator class and then a set of state specific tax calculator classes that the calculator class references internally. Something like:
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)
The directory structure I'm thinking about looks like this:
/calculators/__init__.py
/calculators/ca.py
/calculators/ny.py
And the __init__.py housed the "TaxCalculator" function.
Classes would reference the calculators ie:
from calculators import TaxCalculator
calculator = TaxCalculator().calculate(purchase_info)
Is that considered bad practice or not really Pythonic?
Typically the __init__.py
file is used to initialize your package, like import stuff, add location to your path, defining versions or expose things with __all__
, but often it's also empty.
However I recommend you to give bigger classes/functions it's own files. It's much more maintainable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With