Answer the question
In order to leave comments, you need to log in
What is the best way to initialize "internal" objects?
I have a certain class Application, in which a number of objects are defined inside that describe the application itself directly: a set of routes + views for them. It is natural to add support for middlewars, so that before calling the necessary handler for a request, perform a number of preprocessing actions (for example, check / add a token). For these middlewars, I need an initialization that, for example, would take either user settings (if defined) or default ones that I specify (for example, store tokens somewhere in memory if the user does not want to store in the database (or did not specify )).
The question is how to solve this problem beautifully and elegantly? So far it has occurred to me:
1) Forward the arguments that came to the Application class, and from it, inside, into the depth (that is, all constructors have a signature:
def __init__(self, *args, **kwargs):
# берем какие-то настройки из аргументов/словаря и делаем нужное
....
if __name__ == '__main__':
cmd = CommandLine()
cmd.define('-ip', default='127.0.0.1', help='used ip', type=str)
cmd.define('-port', default=8080, help='listened port', type=int)
args = cmd.parse_command_line()
app = Application()
app.run(ip=args.ip, port=args.port, router=router)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question