Today I learnt how to make a Python module from which you can import anything.
Yuvi asked if you
can write a Python module that will allow anything to be imported from it. A bit
from idealmock import whatever, does_not_exist.
I thought "how hard can it be?" and off I went to find out.
It is surprisingly simple:
def __getattr__(name): pass
If you put this code in your module, say
blackhole.py you can import
anything you want from it:
from blackhole import something, does_not_exist
or you can import the whole module and then access anything you want:
import blackhole; blackhole.something.
It is pretty neat.
If you want to learn more about module level
__getattr__ and some real world
use cases checkout PEP 562 -- Module
which has a nice trick for dealing with deprecated functions in it.