您的位置:首页 > 编程语言 > Python开发

Python 3.3 Tutorial Notes - 4:Modules

2015-09-04 23:57 946 查看
1. File name is the module name2. where you call the "import", where the module will be put into the destination file. This means, if a module contains some code out of any functions, it will be executed in the destination file where it is imported. The execution happens only at the first "import".3. from <lib> import <method1_name [, method2_name, ...]>4. from lib import *, it will import every names except those beginning with an underscore(_)5. "from lib import *" should be avoid except in interactive mode.6. check __name__ if "__main__" to know if current model is main or imported by others.7. Script parameter: sys.argv[1] (the first parameter, 0 is the command line itself)8. Module search path (in below order)     8.1 build-in modules     8.2 path defined in sys.path (need import sys)9. sys.path is initialized from these locations:? the directory containing the input script (or the current directory).? PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).? the installation-dependent default.10.*.pyc and *.pyo, byte code     10.0 The module compileall can create .pyc files (or .pyo files when -O is used) for all modules in a directory. http://docs.python.org/3.3/library/compileall.html                python -m compileall <path>     10.1 loading will be faster, but not running     10.2 -O and -OO: optimization     10.3 it is ok to have only *.pyc or *.pyo but no *.py. This is good to avoid reverse engineer.11. The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings     11.1 dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module builtins12. Package     12.1 The __init__.py files are required to make Python treat the directories as containing packages     12.2 __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable     12.3 "from <package name> import <item>"      12.4 Define the list "__all__" in "__init__.py". It is the list of submodules should be imported by "from *". Then "from <package_name> import *" will only import the name defined in "__all__".     12.5 If "__all__" is not defined, "from <package_name> import *" only import <package name> without sub-modules.      12.6 "from Package import *" is bad practice. "from Package import specific_submodule" is recommended.     12.7 Relative import: leading dots to indicate the current and parent packages involved in the relative import: from . import echo     12.8 "__path__": a list containing the name of the directory holding the package’s __init__.py. Not often needed.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: