"Thomas Jollans" wrote in message news:53faf0ef-4054-53fa-6179-a862495ea...@tjol.eu...

On 2018-08-14 09:38, Frank Millman wrote:
> Hi all
>
> Pylint is flagging a lot of lines as errors that I would consider to be
> acceptable.
>
> I have an abstract class ClassA with a number of concrete sub-classes.
> ClassA has a method which invokes 'self.method_b()' which is defined
> separately on each sub-class. Pylint complains that "Instance of
> 'ClassA' has no  'method_b' member".
>
> First question - as a matter of style, is Pylint correct? If so, I could
> define 'method_b' in ClassA and raise NotImplementedError. Is this
> considered more pythonic? The downside is that I have quite a few of
> them, so it would add some clutter.

I wouldn't say it's unpythonic per se, but if your ClassA is logically
an abstract base class that requires certain methods to be implemented
in subclasses, then it's probably clearer to use Python's abc [1]
facilities, and declare your abstract methods as actual abstractmethods.


[1]: https://docs.python.org/3/library/abc.html


Thanks for the pointer - I will look into that alternative.

[...]


You *can* raise NotImplementedError in your abstractmethods, but I don't
think it's really necessary. You need a statement in the method body of
course, but since you're going to put a docstring there anyway (right?),
that's already taken care of.


At first I thought that it would be necessary, but then I saw in the docs that an ABC class "cannot be instantiated unless all of its abstract methods and properties are overridden", so there is no danger of forgetting to add it to the subclass.

Frank


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to