imported modules becomes None when replacing current module in sys.modules using a class object

后端 未结 1 1889
广开言路
广开言路 2020-12-20 15:47

an unpopular but \"supported\" python hack (see Guido: https://mail.python.org/pipermail/python-ideas/2012-May/014969.html) that enables __getattr__ usage on mo

相关标签:
1条回答
  • 2020-12-20 16:37

    The problem you are running in to is that in Pythons prior to 3.4 when a module is destroyed (as yours is because you replace it with a class and there are no further references to it), the items in that module's __dict__ are forcibly set to None.

    The workaround, if you need to support Pythons prior to 3.4, is have an import statement in the class that will replace the module:

    class MyClass(object):
    
        import os
    
        def check_os(self):
            print(os)
    

    For more info, see this answer about interpreter shutdown.

    0 讨论(0)
提交回复
热议问题