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.
相关文章推荐
- Python 3.3 Tutorial Notes - 3:Data Structure
- Python 3.3 Tutorial Notes - 2:Control Flow
- Python 3.3 Tutorial Notes - 1:Basic
- Python 3.3 Tutorial - Online Resource
- webdriver控制浏览器滚动条 selenium_python
- Python注意事项
- 线程之间的通信
- 使用队列让线程同步
- Python使用条件变量保持线程同步
- Python简单线程同步
- Python线程的daemon属性
- Python线程的线程名
- Python线程isAlive方法
- [转]Python traceback
- Python初学(7)——Python中的作用域基础
- 【Python系列3】两个txt文件通过id进行关联
- python解释器快捷键
- Python语言编程学习资料(电子书+视频教程)下载汇总
- Python新手初学教程
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)