What is the most Pythonic way to deal with a module in which methods must be called in a certain order?
As an example, I have an XML configuration that must be read before doing anything else because the configuration affects behavior.
The parse_config()
must be called first with the configuration file provided. Calling other supporting methods, like query_data()
won't work until parse_config()
has been called.
I first implemented this as a singleton to ensure that a filename for the configuration is passed at the time of initialization, but I was noticing that modules are actually singletons. It's no longer a class, but just a regular module.
What's the best way to enforce the parse_config
being called first in a module?
It is worth noting is that the function is actually parse_config(configfile)
.
If the object isn't valid before it's called, then call that method in __init__
(or use a factory function). You don't need any silly singletons, that's for sure.