I have two data structures, Frontier
(a queue) and Explored
(a set). I want to implement a custom __contains__
method that they share:
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 that making Custom_Structure
an abstract class is appropriate here, because I don't intend it to be instantiated; it is only there so that Frontier
and Explored
can share the __contains__
method. I don't need any abstract methods, but that's ok because I've read that an abstract class doesn't necessarily need abstract methods.
If I was doing this in C# I'd just add the abstract
keyword, and I wouldn't be asking this question. But in Python (2.7) it seems I need to do something like this:
from abc import ABCMeta
class Custom_Structure:
__metaclass__ = ABCMeta
...
Ok, it's still not that big a deal, but it's not very readable, and it just feels like overkill with not much benefit. I'm tempted to just leave my code as it is.
So my question is: Why bother going to the trouble of making Custom_Structure
abstract? (So what if someone instantiates the base class?)
I've found the following questions already but they don't quite answer the question:
Why use Abstract Base Classes in Python? The answer here doesn't seem to apply because I don't have any abstract methods, so there is no contract between the base class and subclasses.
What is the main advantage of making a class abstract Same
Abstract class with no abstract methods This has the answer 'to prevent instantiation', but why is that such a problem?
What's the point in having an abstract class with no abstract methods? Same
Why use an abstract class without abstract methods? Same. I understand the theoretical concept of why it shouldn't be instantiated, but what's the big deal practically?
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 if I'm mistaken please tell me), it's more 'what is the benefit' in this simple case?