Mercurial > hg > config
comparison python/example/abstract.py @ 525:37f8bc525888
-> example
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 24 Sep 2013 12:32:50 -0700 |
parents | python/abstract.py@fe8befc5bdfc |
children | 9149b35b8a2a |
comparison
equal
deleted
inserted
replaced
524:dbe9086643bf | 525:37f8bc525888 |
---|---|
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 c.foo(1) | |
37 a.foo(1) | |
38 | |
39 |