Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to put common methods to an abstract class in Python?

I'm using the abc module to define an interface that subclasses must support. There're also some common methods that are present in all subclasses. Is it ok to put them in the abstract class or should that only contain abstract methods (i.e. decorated with @abc.abstractmethod) ?

like image 971
planetp Avatar asked Nov 08 '18 17:11

planetp


People also ask

Should abstract class implement all methods?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

Do you have to implement all methods of an abstract class Python?

Yes, you have to implement all abstract methods in Python to instantiate them as objects (the ones marked with @abstractmethod , etc). How you implement these, however, are completely up to you. If you're not going to be instantiating, you don't need to override all of them.

Is it necessary for abstract class to have abstract method in Python?

An abstract class may or may not include abstract methods. Python doesn't directly support abstract classes. But it does offer a module that allows you to define abstract classes. To define an abstract class, you use the abc (abstract base class) module.

Can a class have 2 abstract methods in Python?

The following example contains an abstract class, which contains two methods one is an abstract method and another one is a non-abstract method, and the child class is responsible to provide an implementation to the non-abstract class, and it is already given an implementation also, and hence we can perform ...

What is an abstract class in Python?

Abstract Classes in Python. Last Updated: 01-04-2020. An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class.

What is the use of abstract method in it?

It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation.

Is it possible to call an abstract method in Python?

For example: Actually an abstract method is not needed to be “totally abstract” in Python, which is different with some other object-oriented programming language. We can define some common stuff in an abstract method and use super () to invoke it in subclasses.

What is the difference between concrete and abstract classes in Java?

Concrete classes contain only concrete (normal)methods whereas abstract classes may contain both concrete methods and abstract methods. The concrete class provides an implementation of abstract methods, the abstract base class can also provide an implementation by invoking the methods via super ().


Video Answer


1 Answers

TL; DR; Yes, it is OK for an abstract class to have non-abstract methods.

Typically what we call an abstract class is just a class that cannot be instantiated.

On the other hand what we call an interface is a class which has only method declarations but no implementations. In particular its an abstract class because it doesn't have a constructor.

Of course in Python there are no real interfaces: every method has to have a body. But we can somewhat emulate interfaces via raise NotImplementedError().

Anyway interfaces form a subset of abstract classes. This obviously suggests that there are abstract classes that are not interfaces. This is exactly the case you are describing. Yes, abstract class may contain implemented, non-abstract methods. And it is not a bad practice. This is especially useful when a given method does not depend on concrete implementation.


For example consider an interface for a generic parser (I'm thinking about json.load and json.loads):

class ILoader(ABC):
    @abstractmethod
    def load(self, stream):
        raise NotImplementedError()

It's completely OK to give loads method which accepts a string instead of stream with a default implementation:

class AbstractLoader(ABC):
    @abstractmethod
    def load(self, stream):
        raise NotImplementedError()

    def loads(self, text):
        stream = io.StringIO(text)
        return self.load(stream)

although I would use Abstract prefix instead of I. ;)

like image 158
freakish Avatar answered Nov 10 '22 06:11

freakish