一、pycharm
1.1 鼠标悬停在函数名上大概1-2秒钟或Ctrl+Q
或者如果您不想等待,直接光标放在函数名上后,按Ctr+Q快捷键, 也可以迅速显示这个注释窗口。
1.2 鼠标光标落在函数/方法的括号内时使用快捷键Ctrl+P定位当前光标输入的参数
1.3 鼠标光标悬停在函数的名字或参数上,按住Ctrl键+鼠标左键单击。
其实这种方法就是从源头上查看某内置函数、某模块对应的代码。
如何知道这个代码文件所在路径呢?请用【__file__】魔法命令(详见Python __file__属性:查看模块的源文件路径)。
以 string 模块为例:
import string
print(string.__file__) # 输出D:\python3.6\lib\string.py
但请注意,并不是所有模块都提供 __file__ 属性,因为并不是所有模块的实现都采用python语言,有些模块采用的是其它编程语言(如 C 语言)。
1.4 打印help函数的返回结果。
下面以OpenCV-python的findContours函数为例。
print(help(cv2.findContours))
#输出结果如下
Help on built-in function findContours:
findContours(...)
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
. @brief Finds contours in a binary image.
.
. The function retrieves contours from the binary image using the algorithm @cite Suzuki85 . The contours
. are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the
. OpenCV sample directory.
. @note Since opencv 3.2 source image is not modified by this function.
.
. @param image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero
. pixels remain 0's, so the image is treated as binary . You can use #compare, #inRange, #threshold ,
. #adaptiveThreshold, #Canny, and others to create a binary image out of a grayscale or color one.
. If mode equals to #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
. @param contours Detected contours. Each contour is stored as a vector of points (e.g.
. std::vector<std::vector<cv::Point> >).
. @param hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It has
. as many elements as the number of contours. For each i-th contour contours[i], the elements
. hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices
. in contours of the next and previous contours at the same hierarchical level, the first child
. contour and the parent contour, respectively. If for the contour i there are no next, previous,
. parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
. @note In Python, hierarchy is nested inside a top level array. Use hierarchy[0][i] to access hierarchical elements of i-th contour.
. @param mode Contour retrieval mode, see #RetrievalModes
. @param method Contour approximation method, see #ContourApproximationModes
. @param offset Optional offset by which every contour point is shifted. This is useful if the
. contours are extracted from the image ROI and then they should be analyzed in the whole image
. context.
None
1.5 利用__doc__函数实现注释调用。
print(cv2.findContours.__doc__)
#下面是输出;和上面的用help函数来调用注释的结果一样
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
. @brief Finds contours in a binary image.
.
. The function retrieves contours from the binary image using the algorithm @cite Suzuki85 . The contours
. are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the
. OpenCV sample directory.
. @note Since opencv 3.2 source image is not modified by this function.
.
. @param image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero
. pixels remain 0's, so the image is treated as binary . You can use #compare, #inRange, #threshold ,
. #adaptiveThreshold, #Canny, and others to create a binary image out of a grayscale or color one.
. If mode equals to #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
. @param contours Detected contours. Each contour is stored as a vector of points (e.g.
. std::vector<std::vector<cv::Point> >).
. @param hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It has
. as many elements as the number of contours. For each i-th contour contours[i], the elements
. hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices
. in contours of the next and previous contours at the same hierarchical level, the first child
. contour and the parent contour, respectively. If for the contour i there are no next, previous,
. parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
. @note In Python, hierarchy is nested inside a top level array. Use hierarchy[0][i] to access hierarchical elements of i-th contour.
. @param mode Contour retrieval mode, see #RetrievalModes
. @param method Contour approximation method, see #ContourApproximationModes
. @param offset Optional offset by which every contour point is shifted. This is useful if the
. contours are extracted from the image ROI and then they should be analyzed in the whole image
. context.
二、jupyter notebook
2.1 Shift+Tab显示帮助信息
对于函数或方法,一定要把光标放在函数的圆括号内,再使用这个快捷键。
2.2 双问号获取注释帮助信息
在jupyter notebook中,要想获得注释信息,除了help函数、dir函数、__doc__魔法方法等渠道,还可以用双问号??来获取。
注意,单问号只调用源码的docstring部分,但是双问号会调取源码的所有内容(包括docstring)。
比如tf.constant的docstring:
而双问号,调取到的内容,明显更丰富,更多。
2.3 help函数、__doc__函数或者dir函数
比如在import numpy as np的基础上,运行dir(np) 就会显示在numpy包下的所有子模块。
对某一个函数运行help,就可以显示函数的对应帮助信息。
三、学习、调试、开发过程常用到的几种方法
3.1 dir(A)函数或A.__dir__()魔法方法
对于某一个模块、类、函数,可以用dir()或__dic__()来显示它的所有属性、方法。
3.2 A.__dict__属性
相比3.1的结果,__dict__并不会返回父类的属性、方法;但是能多余显示key对应的值。