an unpopular but "supported" python hack (see Guido: https://mail.python.org/pipermail/python-ideas/2012-May/014969.html) that enables __getattr__
usage on module attributes involves the following:
import os, sys
class MyClass(object):
def check_os(self):
print os
sys.modules[__name__] = MyClass()
On import, this the imported module becomes a class instance:
>>> import myModule
>>> myModule
<myModule.MyClass object at 0xf76def2c>
However, in Python-2.7, all other imported modules within the original module is set to None.
>>> repro.check_os()
None
In Python-3.4, everything works:
>>> repro.check_os()
<module 'os' from '/python/3.4.1/lib/python3.4/os.py'>
This feels like something to do with Imported modules become None when running a function, but, anyone knows why this happens internally?
It seems that if you store the original module (without fully replacing it in Python-2) then everything continues to work:
sys.modules[__name__+'_bak'] = sys.modules[__name__]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…