# HG changeset patch # User Jeff Hammel # Date 1325114581 28800 # Node ID 5b82653ccda3937688ad204bd3c1a379daefeb01 # Parent 8e43a4f50e78f9ea19d3724e0fac313eeae967b2 add demonstration of abc error diff -r 8e43a4f50e78 -r 5b82653ccda3 python/abstract.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/abstract.py Wed Dec 28 15:23:01 2011 -0800 @@ -0,0 +1,38 @@ + +def abstractmethod(method): + line = method.func_code.co_firstlineno + filename = method.func_code.co_filename + def not_implemented(*args, **kwargs): + raise NotImplementedError('Abstract method %s at File "%s", line %s should be implemented by a concrete class' % (repr(method), filename, line)) + return not_implemented + +class AbstractBaseClass(object): + + @abstractmethod + def foo(self, arg): + """foo does such and such""" + + @abstractmethod + def bar(self): + """bar does something else""" + +class ConcreteClass(AbstractBaseClass): + + def foo(self, arg): + print 'hello' + +if __name__ == '__main__': + c = ConcreteClass() + c.foo(1) + a = AbstractBaseClass() + try: + a.foo(1) + except NotImplementedError, e: + print e + try: + a.bar() + except NotImplementedError, e: + print e + a.foo(1) + +