3

Here is my code:

from abc import ABC
from abc import abstractmethod

class Mamifiero(ABC):
    """docstring for Mamifiero"""
    def __init__(self):
        self.alimentacion = 'carnivoro'
    
    @abstractmethod
    def __respirar(self):
        print('inhalar... exhalar')
    
class Perro(Mamifiero):
    """docstring for Perro"""
    def __init__(self, ojos=2,):
        self.ojos = ojos

I want that perro.respirar() prints 'inhalar... exhalar' but when I want to instantiate a Perro class show me this error. I want to know what is wrong with my script

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Vinsmoke Mau
  • 327
  • 1
  • 7
  • 18
  • 2
    What did you *think* making an abstract method and not implementing it was going to do? – user2357112 Sep 30 '17 at 17:07
  • Did you actually read what [`@abstractmethod`](https://docs.python.org/3/library/abc.html#abc.abstractmethod) does? – poke Sep 30 '17 at 17:08
  • 1
    An implementation of an abstract method isn't meant to be inherited; rather, it can be called via something like `super().__respirar()` from a non-abstract override. – chepner Sep 30 '17 at 17:23

2 Answers2

3

By definition (read the docs), an abstract call is a class which CANNOT be instantiated until it has any abstract methods not overridden. So as in the Object-Oriented Programming by design.

You have an abstract method Perro.__respirar() not overridden, as inherited from the parent class. Or, override it with a method Perro.__respirar(), and do something there (maybe even call the parent's method; but not in case it is private with double-underscore, of course).

If you want to instantiate Perro, just do not make that method abstract. Make it normal. Because it also has some implementation, which suggests it is a normal base-class'es method, not an abstract method.

Sergey Vasilyev
  • 3,919
  • 3
  • 26
  • 37
0

You need to override __respirar() abstract method in Perro class as shown below:

from abc import ABC
from abc import abstractmethod

class Mamifiero(ABC):
    """docstring for Mamifiero"""
    def __init__(self):
        self.alimentacion = 'carnivoro'
    
    @abstractmethod
    def __respirar(self):
        print('inhalar... exhalar')
    
class Perro(Mamifiero):
    """docstring for Perro"""
    def __init__(self, ojos=2,):
        self.ojos = ojos
    
    # You need to override
    def __respirar(self):
        print('Hello')
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129