Python – Why is it important to prevent instantiating theoretically abstract classes?

Why is it important to prevent instantiating theoretically abstract classes?… here is a solution to the problem.

Why is it important to prevent instantiating theoretically abstract classes?

I have two data structures, Frontier (a queue) and Explored (a collection). I want to implement the custom __contains__ method they shared:

class Custom_Structure:
    def __contains__(self, item):
        # TODO         

class Frontier(Custom_Structure):
    def __init__(self):
        self.queue = Queue.Queue()

class Explored(Custom_Structure):
    def __init__(self):
        self.set = set()

I

understand the theory of inheritance and abstract classes (or at least, I think I do!). It seems appropriate to make Custom_Structure an abstract class here, since I’m not going to instantiate it; Only then can Frontier and Explored share __contains__ methods. I don’t need any abstract methods, but that’s okay because I’ve read that abstract classes don’t necessarily need abstract methods.

If I did this in C#, I would just add the abstract keyword and I wouldn’t ask this question. But in Python (2.7) I seem to need to do something like this:

from abc import ABCMeta

class Custom_Structure:
    __metaclass__ = ABCMeta
    ...

Well, it’s still not a

big deal, but it’s not very readable and feels a bit overkill without much benefit. I’d love to keep my code as is.

So my question is: why abstract Custom_Structure? (What if someone instantiates the base class?) )

I’ve found the following questions, but they don’t quite answer them:

Why use Abstract Base Classes in Python? The answer here doesn’t seem to apply, since I don’t have any abstract methods, so there is no contract between the base class and the subclass.

What is the main The advantage of making a class abstract is the same

Abstract class with no abstract The method answer is “prevent instantiation”, but why does such a question arise?

What’s the point in having an abstract class with no abstract methods? Same

Why use an abstract class without abstract methods? The same. I understand why its theoretical concept should not be instantiated, but what’s the big deal in reality?

I’ve read various questions and answers about the theory behind abstract classes – my question is not about what they are and how they work (although let me know if I’m mistaken), it’s more about “what’s the benefit” in this simple case?

Solution

Interesting question. Let me start by saying that this ugly thing is the way abstract classes are created in python2….

from abc import ABCMeta

class Custom_Structure:
    __metaclass__ = ABCMeta

… It has been shown that Python dictators don’t think it’s that important to mark classes as explicit abstractions.

That said, abstracting a class is a useful way to add metadata to your code. This is a way for the author of the class to communicate with the future user of the module.

What are you talking about? Two things:

  1. Abstract classes are incomplete and should not be instantiated. Examples of this class have no specific meaning because the definition is partial. It lacks the full set of information needed to make the object work.

  2. Abstract methods are the missing pieces in the definition. By abstracting a method, you are declaring that for the class to be meaningful, you must provide a complete description of this particular part. Not only that, but by providing an interface, you are also communicating the expected behavior of the missing parts, regardless of what the internal details end up being.

Of course, all of this may be the case in practice without any additional specification of the class or its methods, but the inheritors end up guessing how to use the object, or have to read detailed documentation.

If you implicitly instantiate an abstract class, sooner or later an error will occur. When something is missing, you may end up with a NotImplementedError or KeyError. This mistake is easy to make and appears when trying to use the object’s functionality.

If you try to instantiate an abstract class explicitly, you get an immediate error. Don’t roll the dice, and whenever the class is used in some way, the problem is not postponed. The lack of an implementation of an abstract method does not cause an error when and if the method is used, but it causes an error immediately when an encoding error occurs.

In summary, making a class and some of its methods explicitly abstract can keep the authenticity of the code consistent with the documentation. It shifts the responsibility of knowing the rules from inexperienced users to authors. This is a way for the language runtime to enforce theoretical rules and usage constraints in practice.

Related Problems and Solutions