Of course "no consequences except the obvious" is a valid answer to my question about the consequences.
I help maintain a code base, with some proposed code structured roughly like this:
# main.sh
export PYTHONPATH=$PYTHONPATH:$(pwd)
python main.py "brazil"
python main.py "france"
# main.py
from importlib.util import find_spec, module_from_spec
import sys
class BaseProcessor(object):
# abstract base class
pass
def run_for(country):
spec = find_spec(country + ".processor")
module = module_from_spec(spec)
spec.loader.exec_module(module)
processor = module.CountryProcessor()
processor.do_something()
if __name__ == "__main__":
run_for(sys.argv[1])
# brazil/processor.py
from main import BaseProcessor
class CountryProcessor(BaseProcessor):
def do_something(self):
print("This is the Brazil CountryProcessor")
# france/processor.py
from main import BaseProcessor
class CountryProcessor(BaseProcessor):
def do_something(self):
print("This is the France CountryProcessor")
The code in run_for() is using importlib to find a module named processor based on the passed-in string country.
Output is as designed:
> ./main.sh
This is the Brazil CountryProcessor
This is the France CountryProcessor
We have some similar code (but using sys.path.insert()) that has already been running for months; I don't know of any situation where the same-named classes have caused problems.
What, if any, could be the unintended consequences arising from this design?