I've seen (and once used) this idiom a few times in Python:
# At the end of a module: if __name__ == '__main__': main() # or whatever your entry point looks like, this line isn't important else: sys.modules[__name__] = SomeClass()
SomeClass will implement one or more magic methods, to allow code like this:
import mod with mod: pass foo = mod[bar] mod() # etc.
In my specific case, the module had previously acted a lot like an immutable mapping (having a top-level
lookup() function of one argument), and I wanted to provide it with standard syntax for iteration, key lookup, etc. I felt this made more sense than writing several top-level functions that implemented all the same features as a mapping but with less familiar names.
I suppose my question is whether this is a pattern or an antipattern. Singletons themselves are often bad design, but if you're going to be using a module-as-singleton anyway, is this a good way to implement it?