Home>Article>Backend Development> How to write python header file
This article mainly uses python2 as an example. First, I will introduce the programming style of Python header files, and then I will give you a detailed introduction to the basic usage of the import part. These two parts are the components of the header file in Python.
Programming style
#!/usr/bin/env python #在文件头部 ( 第一行 ) 加上 设置 Python 解释器 # -*- coding: utf-8 -*- #在文件头部 ( 第二行 ) 加上 在编辑器中设置以 UTF-8 默认编码保存文件 # Copyright (c) *** #版权信息放在文件头部,文件编码之后 # docstring 是Python特有的注释风格,它是模块、类或函数的第一个语句,可以通过__doc__方法访问 # 用英语撰写注释,短注释可以忽略末尾的句号 (.) # docstring 为每个模块、类或函数撰写 docstring 推荐使用三个双引号(”””) 来定义 docsting , 不推荐使用三个单引号 (''') # 模块的 docsting 放在文件头部,版权信息之后 ”””This is a one line docstring.””” ”””The title of a multiline docstring: After title is the content.You can write it as long as needed. ””” # 把 import 语句放在文件头部,在模块 docstring 之后,在模块全局变量或全局常量之前 # 按照从一般到特殊的原则分组 import 语句,先 import 标准库,然后 import 第三方库,最后 import 程序中的自定义库 # 在不同分组的 import 语句之间加空行 # 每个 import 语句只导入一个模块 import sys import time from PyQt.QtCore import SIGNAL, QTimer # 在终端中使用中文字符 在文件头部加上 # import sys # reload(sys) # sys.setdefaultencoding('utf-8')
Related recommendations: "Python Video Tutorial"
import
1. Application of system libraries
The system libraries mentioned here generally refer to the libraries that come with the python software, not libraries installed from third parties. For example, if sys is imported, how will it be printed?
>>> import sys >>> print sys
This shows that the sys library is probably embedded in python and there is no way to delete it. Are all libraries like this? We can try os:
>>> import os >>> print os
Obviously, the os library here is provided by pyc, so in terms of priority, it seems that sys is still needed More important.
2. Third-party libraries
The so-called third-party libraries are libraries installed through python setup.py install, pip installation or apt-get install. There are many such libraries. The ones I commonly use include webpy, twisted, cv2, sklearn, and wxpython. These libs, without exception, all exist in the form of pyc. For example, let's take a look at webpy:
>>> import web >>> print web
3. Local directory file reference
If it is a local file reference, the method is actually very simple, just import it directly. If the import is successful, python will generate the pyc file of the corresponding file. For example, if exer1.py wants to reference the function in exer2.py, then it can do this:
import exer2
or
from exer2 import *
Secondly, it should be noted that this method is not only applicable For python, it also applies to dynamic library files. We know that sometimes in order to expand python functions, we need to write c files to generate dynamic libraries for python to use. Interested students can refer to this link. At this time, if you need to reference a dynamic library file, just import the file name directly.
4. Reference subdirectory files
In addition to sibling directory references, subdirectory references are also commonly used. For example, exer1.py and the sub directory are in the same folder. If exer1.py wants to reference exer2.py under sub, what should it do? At this time, be sure to ensure that there is an __init__.py file in the sub directory.
from sub import exer2
Or,
from sub.exer2 import *
5. The subdirectory refers to the parent directory
Sometimes, the subdirectory uses some of the parent directory or other directories (similar to the util directory) Functions often require backreferences. At this time, the sys library comes in handy. Assume that exer1.py and the sub directory are at the same level, and there is an exer2.py file in the sub directory. At this time, exer2.py wants to reference the functions of exer1.py, then it can do this:
import sys sys.path.append('../') import exer1
Or,
import sys sys.path.append('../') from exer1 import *
6. Cross-reference
If there are two files that are cross-referenced, I suggest you keep the reference of one file at this time, and put the reference of the other file under the function, for example Say something like this:
def process(): import exer1 exer1.add()
7. Suggestions on the order of import files
Regarding the order of import files under python, my personal experience is that this arrangement is more reasonable,
built -in system library
Other system library
Third-party library
Subdirectory library
Other local files, etc.
The above is the detailed content of How to write python header file. For more information, please follow other related articles on the PHP Chinese website!