Re:Say “no” to import side‐effects in Python
by halostatue on Sunday April 06, 2014 @10:47AM
(#7541108)
Thinking about it, I do the same thing with my Ruby library mime-types[1]. The library is only useful if you load some data into its registry, so it does so automatically, unless you specify RUBY_MIME_TYPES_LAZY_LOAD in the environment.
I've just put myself a task to reverse the behaviour so that lazy loading is the default.
Mind you, my library does not connect to a database, or any such crazy thing. It simply creates a singleton which is then useful throughout your application. There would be very few cases where you would not want that singleton and you would want your own instance. If so, you are free to completely ignore the created singleton and make your own instance. The creation process is also idempotent, except the data you put into that singleton; that data is a special case: it is your explicit responsibility to namespace it, which is indeed the whole point of this library. I feel pretty good about this thing.
[1] https://github.com/ipartola/groper