我有
class A(object):
def __init__ (self): raise NotImplementedError("A")
class B(A):
def __init__ (self):
....
Run Code Online (Sandbox Code Playgroud)
和pylint说
__init__ method from base class 'A' is not called
Run Code Online (Sandbox Code Playgroud)
显然,我不想这样做
super(B, self).__init__()
Run Code Online (Sandbox Code Playgroud)
(我试过abc并得到了
Undefined variable 'abstractmethod'
Run Code Online (Sandbox Code Playgroud)
从pylint,所以这也不是一个选项).
使用abc对我有用:
import abc
class A(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def __init__(self):
pass
class B(A):
def __init__(self):
super(B, self).__init__()
Run Code Online (Sandbox Code Playgroud)
abc我收到警告,但与父母没有任何关系或__init__没有被调用:
C: 1, 0: Missing module docstring (missing-docstring)
C: 3, 0: Invalid class name "A" (invalid-name)
C: 3, 0: Missing class docstring (missing-docstring)
R: 3, 0: Too few public methods (0/2) (too-few-public-methods)
C: 9, 0: Invalid class name "B" (invalid-name)
C: 9, 0: Missing class docstring (missing-docstring)
R: 9, 0: Too few public methods (0/2) (too-few-public-methods)
R: 3, 0: Abstract class is only referenced 1 times (abstract-class-little-used)
Run Code Online (Sandbox Code Playgroud)
就其价值而言,我支持@holdenweb。有时你比 pylint 更了解。