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
) ?
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.
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.
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.
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 ...
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.
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.
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.
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 ().
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
. ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With