我应该如何注释 numpy.ndarray 形状几乎不变的对象 (Python)
How should I annotate numpy.ndarray object whose shape is almost constant (Python)
最近我注意到在脚本中留下评论或某种解释是多么重要。
所以我决定在我的 python 脚本中添加注释来处理图像对象。
那么,我应该如何注释图像对象或只是 numpy.ndarray 对象?
我想澄清一下,函数的输入图像必须有 3 个通道。
这是我当前的示例代码。但是我不满意。
def proc(image: numpy.ndarray) -> numpy.ndarray:
print("hi")
return image
对不起,我的英语不好,请给我一些建议。
您的代码应该是 self-documenting,即遵循 PEP 8. Type hinting and expressive identifier names help reason about your code but you should not be limited to that. You can add comments and docstrings 中描述的命名和结构化约定到您的脚本中以添加更多信息。可以使用特定的文档字符串格式(reStructuredText、NumPy...),但您应该在整个项目中坚持使用相同的格式。
这是一个示例(遵循 NumPy 文档字符串格式):
def proc(image: numpy.ndarray) -> numpy.ndarray:
"""This function takes an input image, apply transformation X
and return the transformed image.
Parameters
----------
image : numpy.ndarray
Input images must have 3 channels.
Returns
-------
numpy.ndarray
The transformed image.
"""
# processing code here with comments
return image
最近我注意到在脚本中留下评论或某种解释是多么重要。 所以我决定在我的 python 脚本中添加注释来处理图像对象。
那么,我应该如何注释图像对象或只是 numpy.ndarray 对象? 我想澄清一下,函数的输入图像必须有 3 个通道。
这是我当前的示例代码。但是我不满意。
def proc(image: numpy.ndarray) -> numpy.ndarray:
print("hi")
return image
对不起,我的英语不好,请给我一些建议。
您的代码应该是 self-documenting,即遵循 PEP 8. Type hinting and expressive identifier names help reason about your code but you should not be limited to that. You can add comments and docstrings 中描述的命名和结构化约定到您的脚本中以添加更多信息。可以使用特定的文档字符串格式(reStructuredText、NumPy...),但您应该在整个项目中坚持使用相同的格式。
这是一个示例(遵循 NumPy 文档字符串格式):
def proc(image: numpy.ndarray) -> numpy.ndarray:
"""This function takes an input image, apply transformation X
and return the transformed image.
Parameters
----------
image : numpy.ndarray
Input images must have 3 channels.
Returns
-------
numpy.ndarray
The transformed image.
"""
# processing code here with comments
return image