Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
# conf.py
if sys.environ.get('ENV_TYPE') == 'PROD':
db = ...
mailer = ...
else:
db = ...
mailer = ...
# main.py
import conf
foo(conf.db)
bar(conf.mailer)
class User(object):
db = Fake() # db connection
def do_some():
do.save(True)
.....
User.db = RedisDb('localhost:1234') # explicit injection or 'setattr' can be used for text based config files
user = User()
user.do_some() # the redis db is used
class Container:
def __init__(self, system_data):
for component_name, component_class, component_args in system_data:
if type(component_class) == types.ClassType:
args = [self.__dict__[arg] for arg in component_args]
self.__dict__[component_name] = component_class(*args)
else:
self.__dict__[component_name] = component_class
потому как ту отсутствует строгая типизация
бороться с ней с помощью инъекций бессмысленно
class User(object):
def db_some():
RedisDb('localhost:1234').save(True)
Внедрение зависимостей the Python way