In Python, there is no way to explicitly restrict a class from being instantiated. There are, however, ways to make a Python class implicitly abstract. A common use of abstract classes in Python is to define an interface. For instance, here we define an abstract class and a concrete class.
class AbstractClass(object):
def first(self):
pass
def second(self):
pass
class ConcreteClass(AbstractClass):
def first(self):
print self.__class__.__name__
if __name__ == "__main__":
obj = ConcreteClass()
obj.first()
obj.second()
In this example, AbstractClass defines two methods that aren't implemented. The ConcreteClass class inherits the abstract class but only redefines one method. Yet, we are invoking both methods. So one method does something while the other one does nothing.
This is goo in that the expected interface is supported when the object is put to use, but this approach can lead to bugs that aren't trivial to track down. It isn't at all obvious to the clients of these objects that one of the methods isn't implemented. Consider the following approach.
class AbstractClass(object):
def first(self):
raise NotImplementedError
def second(self):
raise NotImplementedError
class ConcreteClass(AbstractClass):
def first(self):
print self.__class__.__name__
if __name__ == "__main__":
obj = ConcreteClass()
try:
obj.first()
obj.second()
except NotImplementedError:
print "Not implemented. Good to know."
Here, AbstractClass will raise a NotImplementedError instead of doing nothing. This way, it complains to clients of it's instances that it can't do anything with the requested behavior. This certainly makes debugging a lot easier while maintaining the ability to define abstract interfaces for classes.
Hi. This is a good approach. I'm fairly new to Python coming from a Java background. I'm trying to sort of empty by OO glass in this regard. But abstract classes are necessary if something like an 'interface' is not available specifically for this. For my knowledge this is the only type of interface definition in Python. So it is quite reasonable to achieve it like that. Thanks for the insight.
ReplyDeletehttp://madchuckle.blogspot.com
Note that Python 2.6 introduced the concept of abstract base classes via the 'abc' module.
ReplyDeleteSee http://docs.python.org/library/abc.html
Never use it though...
> Never use it though...
ReplyDeleteOoops, I meant "I never used it though..."