unexpected-special-method-signature (PLE0302)
源自 Pylint 代码检查工具。
作用
检查具有意外方法签名的“特殊”方法。
为什么这不好?
诸如 __len__
之类的“特殊”方法应遵循特定的标准函数签名。使用非标准函数签名实现“特殊”方法可能会导致给定类的用户出现意外和令人惊讶的行为。
示例
class Bookshelf:
def __init__(self):
self._books = ["Foo", "Bar", "Baz"]
def __len__(self, index): # __len__ does not except an index parameter
return len(self._books)
def __getitem__(self, index):
return self._books[index]
建议改为
class Bookshelf:
def __init__(self):
self._books = ["Foo", "Bar", "Baz"]
def __len__(self):
return len(self._books)
def __getitem__(self, index):
return self._books[index]