Python abstract classes
May 25, 2007 | co.mments
"The abc module also defines a new decorator, @abstractmethod, to be used to declare abstract methods. A class containing at least one method declared with this decorator that hasn't been overridden yet cannot be instantiated. Such methods may be called from the overriding method in the subclass (using super or direct invocation). "
For anyone involved in large Python code bases, or those working with languages like Java or C# (or Blub) that think Python requires inordinate discipline to scale up structurally on LOC, this is big news. The @abstractmethod decorator will allow the kind of signature based contracts that C# and Java programmers have relied on for years to organize their code.
One downside I see is that it seems you can now break your client users by adding new @abstractmethod to base classes after you release. That is, this feature will introduce the notion of API backward compatibility through method addition as well as deprecation. This is the same issue as adding a new method signature to a Java interface.
a problem that C# notably doesn't have because it only provides abstract classes.
In passing I'll mention that Zope3 introduced the notion of interfaces a few years ago, after the lessons learned from Zope2, which is one of the bigger Python code bases out there. Zope3 seems to avoid the backward compatibility issue - you don't have to implement a method unless a NotImplementedException is declared in the base class' method body - at the expense of having its own inheritance hierarchy via "zope.interface". As PJ Eby said "where Zope leads, Python follows".
May 25, 2007 07:52 PM
Post a comment
TrackBack URL for this entry: