Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

semantics of __module__

Tags:

python

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 will have inside the module. I was wondering if it is a good idea to update the function's __module__ attribute as well to point to the module the function will reside. The docs say __module__ is:

The name of the module the function was defined in, or None if unavailable.

The code that creates the function resides in a different module which is pretty much unrelated to the module where the function resides. There is no reference to the function in this module.

I've done some poking around on the mailing list but I'm a bit confused as to what the semantics of __module__ are and if I should set it to None or the module that the function resides or the module where the code resides that created the function. Gonna leave it be for now but am interested to see if anyone knows the answer.

like image 659
Jason Keene Avatar asked Apr 11 '12 20:04

Jason Keene


People also ask

What does __ module __ mean in Python?

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.

What is __ class __ in Python?

__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .

What is __ main __ class?

__main__ — Top-level code environment. In Python, the special name __main__ is used for two important constructs: the name of the top-level environment of the program, which can be checked using the __name__ == '__main__' expression; and. the __main__.py file in Python packages.

What is __ all __ in Django?

__all__ is used to document the public API of a Python module.


2 Answers

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.

like image 80
alexis Avatar answered Sep 22 '22 20:09

alexis


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.

like image 29
Jakob Bowyer Avatar answered Sep 25 '22 20:09

Jakob Bowyer