Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an Abstract Class inherit from another Abstract Class in Python?

Is it possible to have an Abstract Class inheriting from another Abstract Class in Python?

If so, how should I do this?

like image 267
Squilliam Avatar asked Jan 19 '18 08:01

Squilliam


People also ask

Can an abstract class inherit another abstract class Python?

A class that is derived from an abstract class cannot be instantiated unless all of its abstract methods are overridden.

How do you inherit two abstract classes?

In ABSTRACT class,we can't extends multiple abstract classes at a time. but In INTERFACE, we can implements multiple interfaces at time. Therefore , interfaces are used to achieve multiple inheritance in java.

Can a class inherit from two abstract classes?

A class can inherit from multiple abstract classes.

Can a class have 2 abstract methods in Python?

Note the abstract base class may have more than one abstract methods. The child class must implement all of them failing which TypeError will be raised.


1 Answers

Have a look at abc module. For 2.7: link. For 3.6: link Simple example for you:

from abc import ABC, abstractmethod

class A(ABC):
    def __init__(self, value):
        self.value = value
        super().__init__()

    @abstractmethod
    def do_something(self):
        pass


class B(A):
    @abstractmethod
    def do_something_else(self):
        pass

class C(B):
    def do_something(self):
        pass

    def do_something_else(self):
        pass
like image 109
luminousmen Avatar answered Sep 20 '22 12:09

luminousmen