Welcome, guest | Sign In | My Account | Store | Cart
def abstractmethod(method):
    def default_abstract_method(*args, **kwargs):
        raise NotImplementedError('call to abstract method ' + repr(method))

    default_abstract_method.__name__ = method.__name__
    
    return default_abstract_method


if __name__ == '__main__':

    class A:
        @abstractmethod
        def foo(self, data): pass

    class B(A):
        def foo(self, data):
            self.data = data

    a = A()
    b = B()
    b.foo(5)
    
    exception_raised = False
    try:
        a.foo(3)
    except NotImplementedError:
        exception_raised = True

    assert exception_raised

    print 'OK'

Diff to Previous Revision

--- revision 4 2011-04-19 11:21:03
+++ revision 5 2011-04-20 21:50:23
@@ -1,10 +1,6 @@
-class AbstractMethodError(Exception):
-    def __init__(self, method):
-        Exception.__init__(self, 'call to abstract method ' + repr(method))
-
 def abstractmethod(method):
     def default_abstract_method(*args, **kwargs):
-        raise AbstractMethodError(method)
+        raise NotImplementedError('call to abstract method ' + repr(method))
 
     default_abstract_method.__name__ = method.__name__
     
@@ -28,7 +24,7 @@
     exception_raised = False
     try:
         a.foo(3)
-    except AbstractMethodError:
+    except NotImplementedError:
         exception_raised = True
 
     assert exception_raised

History