comparison python/abstract.py @ 188:5b82653ccda3

add demonstration of abc error
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 28 Dec 2011 15:23:01 -0800
parents
children fe8befc5bdfc
comparison
equal deleted inserted replaced
187:8e43a4f50e78 188:5b82653ccda3
1
2 def abstractmethod(method):
3 line = method.func_code.co_firstlineno
4 filename = method.func_code.co_filename
5 def not_implemented(*args, **kwargs):
6 raise NotImplementedError('Abstract method %s at File "%s", line %s should be implemented by a concrete class' % (repr(method), filename, line))
7 return not_implemented
8
9 class AbstractBaseClass(object):
10
11 @abstractmethod
12 def foo(self, arg):
13 """foo does such and such"""
14
15 @abstractmethod
16 def bar(self):
17 """bar does something else"""
18
19 class ConcreteClass(AbstractBaseClass):
20
21 def foo(self, arg):
22 print 'hello'
23
24 if __name__ == '__main__':
25 c = ConcreteClass()
26 c.foo(1)
27 a = AbstractBaseClass()
28 try:
29 a.foo(1)
30 except NotImplementedError, e:
31 print e
32 try:
33 a.bar()
34 except NotImplementedError, e:
35 print e
36 a.foo(1)
37
38