semantics of __module__

后端 未结 2 652
不思量自难忘°
不思量自难忘° 2021-01-01 17:42

I\'m dynamically defining functions in a module and then updating the module\'s __all__ and the function\'s __name__ attribute to match the name it

相关标签:
2条回答
  • 2021-01-01 17:58

    I wouldn't do it. Importing functions from one module to another is a common occurrence. The __module__ property is intended for retrieving the module where the function was defined, either to read the source code or sometimes to re-import it in a script. I don't see that it makes much difference whether the definition is via static code or dynamically: If someone wants to read your function's source code, they should look in the module that dynamically creates it.

    You could take a look at PEP 3130. Although it was rejected, it might give you more insight into the purpose of __module__ than the single-sentence description found everywhere else.

    0 讨论(0)
  • 2021-01-01 18:08

    You shouldn't have to worry about __module__ usually, sometimes its used for dark magic or knowing where a function came (example) debugging from, but most of the time everyone ignores it. If your really worried set __module__ = "dynamically_defined_function" or something similar.

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