# HG changeset patch # User Jeff Hammel # Date 1380051170 25200 # Node ID 37f8bc525888f9fec898037ca9c7d1676f8319ed # Parent dbe9086643bf2dae003e1d4eee531fc1139be696 -> example diff -r dbe9086643bf -r 37f8bc525888 python/abstract.py --- a/python/abstract.py Tue Sep 24 12:29:59 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ - -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 - c.foo(1) - a.foo(1) - - diff -r dbe9086643bf -r 37f8bc525888 python/example/abstract.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/example/abstract.py Tue Sep 24 12:32:50 2013 -0700 @@ -0,0 +1,39 @@ + +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 + c.foo(1) + a.foo(1) + +