1

这是一个简单的过滤器,我一直在通过串行连接读取数据的项目中使用它,并认为将它用作我第一次尝试编写文档字符串会很好。有没有人有什么建议?我一直在阅读 PEP 257。因为它是一个类,所以关键字参数应该在__init__?

如果有更好的方法来编写它的任何部分(不仅仅是文档字符串),如果人们能指出我正确的方向,我将不胜感激。

class Filter(object) :
    """Return x if x is greater than min and less than max - else return None. 

    Keyword arguments:
    min -- the minimum (default 0)
    max -- the maximum (default 0)

    Notes:
    Accepts integers and integer strings
    """
    def __init__(self, min=0, max=0) :
        self.min = min
        self.max = max
    def __call__(self, input) :
        try :
            if int(input) <= self.max and int(input) >= self.min : 
                return int(input)
        except : pass
4

1 回答 1

0

使用 pylint 和 pep8 检查代码约定和常见的输入错误通常是个好主意。

于 2012-04-26T11:37:30.073 回答