How to get a reference to a module inside the module itself?

前端 未结 6 940
梦毁少年i
梦毁少年i 2020-12-07 11:55

How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module?

6条回答
  •  不知归路
    2020-12-07 12:41

    If you have a class in that module, then the __module__ property of the class is the module name of the class. Thus you can access the module via sys.modules[klass.__module__]. This is also works for functions.

提交回复
热议问题