Original link: https://chegva.com/5445.html
◎Knowledge points
-
Call the built-in function dir to view all properties of the module
-
__doc__ of special attributes of modules
-
__name__ of special attributes of modules
-
Single underscore for data access control within a module
-
Special attribute for data access control within a module _ _all__
◎Script practice
▽ Call the built-in function dir to view all the properties of the module
"""You can call the built-in function dir to view all the attributes of the module 1. When calling the built-in function dir, the module is used as a parameter. When calling the built-in function dir, if the module is used as a parameter, all attributes of the specified module will be returned. Among them, start with double underscore __ and The attribute at the end is the special attribute of the module"""import modprint(dir(mod))"""2. The built-in function dir is called without any parameters. If the built-in function dir is called without any parameters, it will return the current module's All properties in local scope"""print(dir())var = 56def ff(): print('ff was called')class SomeClass(object): passprint(dir())del varprint(dir())
▽ __doc__ of special attributes of modules
"""Among all the attributes of the module obtained by calling the built-in function dir, there is a special attribute called __doc__, which is used to represent the documentation string of the module. 1. What is the documentation string of a module (docstring) and the documentation string of a function Similarly, the string on the first line of a module is called the module's docstring, and is usually denoted by three quotes. A module's docstring is a brief description of the module's functionality. In PyCharm, a module's docstring is grayed out The "doc" string is called "documentation" because tools can be used to automatically generate documentation from docstrings. You should get into the habit of writing docstrings to improve program readability. Opens module base64 in the standard library .py to view its docstring.""""""2. Accessing the module's docstring The module's docstring can be accessed through the module's special attribute __doc__ The help information obtained by calling the built-in function help() will Contains the module's docstring """print(__doc__)import base64print(base64.__doc__)print(help(base64))
▽ The special attribute of the module is __name__
"""Among all the attributes of the module obtained by calling the built-in function dir, there is a special attribute called __name__1, the value of the special attribute __name__ of the module in different situations (1) For the imported module, the special attribute __name__ The value is the module name (2) For a module that is run directly, the value of its special attribute __name__ is __main__2, and it is judged whether to execute the test code in the module according to the value of __name__ For the test code in the module, usually when the module is run directly It only needs to be executed, and it does not need to be executed when the module is imported. Therefore, it can be judged whether to execute the test code in the module """ according to the value of __name__
▽ The single underline of the data access control in the module
""" In order to achieve data access control in a module to some extent, you can add a single underscore _ before some attributes in the module. In this way, you cannot use the statement "from module name import *" to import the corresponding attributes, However, the corresponding properties can still be imported using the statement "import module-name"."""
▽ The special attribute of data access control in the module __all__
""" In order to achieve data access control within the module to some extent, the special attribute __all__ can also be defined in the module. In this way, using the statement "from module name import *" can only import those defined in the special attribute __all__ properties, however, all properties can still be imported using the statement "import modulename"."""""" When importing properties using the statement "from modulename import *", if both are added before an property in the module Single underscore _, and this attribute is defined in the special attribute __all__ in the module, then __all__ has higher priority than single underscore"""
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/module_attributes.py
This article is reprinted from: https://chegva.com/5445.html
This site is for inclusion only, and the copyright belongs to the original author.