Implement abstract method in Python inheritance

Asked

Viewed 1,077 times

4

Be the following class hierarchy, where class "A" has an abstract method called "foo":

# -*- coding: utf-8 -*-
import abc


class A:
    __metaclass__ = abc.ABCMeta

    def __init__(self):
        pass

    @abc.abstractmethod
    def foo(self):
        return


class B(A):
    def __init__(self):
        super(B, self).__init__()
        pass 

    def foo(self):
        print 'Foo method in class B'


class C(B):
    def bar(self):
        print 'Bar method in class C'


def main():
    class_c = C()
    class_c.foo()
    class_c.bar()


if __name__ == "__main__":
    main()

This code works perfectly by printing the following:

"Foo method in class B"
"Bar method in class C"

My doubt is that Pycharm says that class "C" should implement all abstract methods, in this case the methods defined in class "A".

Is there a convention that says I have to implement all abstract methods? Is there a problem with not implementing?

  • 1

    I can almost guarantee it’s bug Pycharm. C implements all methods of A. It’s just not so obvious.

  • "C" inherits from "B" which implements the abstract "foo" method of "A", yet has no problem?

  • 1

    No. I think it is naive of the IDE. It is important to have the method implemented. Only if there is some trick in Python that I don’t know (so I didn’t answer) that creates some difficulty. But it shouldn’t exist.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

  • 2

    I voted to close the question as non-reducible because it was clearly a problem in the IDE (or poor configuration) and cannot be reproduced only with the code snippet..

2 answers

3

Maybe you know this way is not much phytonica. When you want guarantees in the code it’s better to use another language. This is probably why Pycharm complains about this, he doesn’t go very far to check that everything is in order.

So he doesn’t see the method implemented there and thinks it’s a problem. It’s not, the important thing is to have the method implemented, and it was in the class B and will obviously be inherited by C.

0

I need to disagree with the above opinion:

Maybe you know that this form is not very phytonica. When you want guarantees in the code, it is better to use another language.

This is a great confusion that many Python programmers carry out around the language and how to implement certain codes. The fact that Python is flexible and does not require you to follow certain "standards" does not mean that you should not use them or use other languages that "force" the use of the standard, such as Java, for example.

In the case of this post, when you use the ABC class as a metaclass of a class A, you are informing Python that all objects that inherit this class A must "obligatorily" implement all methods of class A. Therefore, Pycharm is complaining.

This type of implementation is often used when building classes that implement the "interface standard".

If you wanted more details you can see this link: There are interfaces in python?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.