Are there abstract classes in Python?

Asked

Viewed 53 times

4

I’m a beginner in Python and would like to know if there are abstract classes and methods in Python, if there is someone can give me an example? When I studied Java there were these classes to create a "model" and they were not instantiable.

1 answer

3


Yes you have, following the documentation here from ABC need to import ABC to create an abstract class:

from abc import ABC

class ClasseAbstrata(ABC):
    @abstractmethod
    def abstract_method(self)

In this case, the ABC module is like "an abstract class to create abstract classes"

To inherit from that class:

  class ClasseDerivada(ClasseAbstrata):
     def abstract_method(self):
          print 'metodo na classe derivada'
  • Does Python support object orientation and only added the concept of abstract class to the language in a later update? That I didn’t know, that "beauty" huh...

  • did not understand the "just added the concept of abstract class", did not say anything about the concept, since this was not asked, I only showed the implementation, can explain better?

  • I went to this link and it said that this support is a PEP that was added to Python, I deduced that it was something after the initial design of the language. In my view on class-based object orientation the concept of abstract class is essential and a language that supports this paradigm should support it "at the root". It was just a comment en passant.

  • ah yes agree, nor should I need to use any package, it is a basic concept of OOP, it seems that was a "pulled" :D

  • 1

    I will talk to Guido Von Rossum to resolve this. : D :D :D ahahahah who I am, he must have had his reasons. It is there explained in PEP.

  • 1

    :D opens an incident to make it better ;)

Show 1 more comment

Browser other questions tagged

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