Mercurial > hg > config
comparison python/example/abstract.py @ 550:9149b35b8a2a
python/example/abstract.py
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 18 Nov 2013 16:17:39 -0800 |
parents | 37f8bc525888 |
children |
comparison
equal
deleted
inserted
replaced
549:3168816f2d28 | 550:9149b35b8a2a |
---|---|
1 #!/usr/bin/env | |
2 | |
3 """ | |
4 demo related to abstract classes | |
5 """ | |
6 | |
7 ### roll our own to demo | |
1 | 8 |
2 def abstractmethod(method): | 9 def abstractmethod(method): |
3 line = method.func_code.co_firstlineno | 10 line = method.func_code.co_firstlineno |
4 filename = method.func_code.co_filename | 11 filename = method.func_code.co_filename |
5 def not_implemented(*args, **kwargs): | 12 def not_implemented(*args, **kwargs): |
19 class ConcreteClass(AbstractBaseClass): | 26 class ConcreteClass(AbstractBaseClass): |
20 | 27 |
21 def foo(self, arg): | 28 def foo(self, arg): |
22 print 'hello' | 29 print 'hello' |
23 | 30 |
31 | |
32 ### now use the abc module | |
33 | |
34 import abc | |
35 | |
36 class AbstractClass(object): | |
37 @abc.abstractmethod | |
38 def foo(self, arg): | |
39 """blah""" | |
40 @abc.abstractmethod | |
41 def bar(self): | |
42 """bar does nothing""" | |
43 | |
44 class CementClass(AbstractBaseClass): | |
45 def foo(self, arg): | |
46 print 'goodbye' | |
47 | |
48 class Picasso(object): | |
49 __metaclass__ = abc.ABCMeta | |
50 @abc.abstractmethod | |
51 def foo(self, arg): | |
52 """blah""" | |
53 | |
54 | |
24 if __name__ == '__main__': | 55 if __name__ == '__main__': |
56 | |
57 # home-rolled | |
25 c = ConcreteClass() | 58 c = ConcreteClass() |
26 c.foo(1) | 59 c.foo(1) |
27 a = AbstractBaseClass() | 60 a = AbstractBaseClass() |
28 try: | 61 try: |
29 a.foo(1) | 62 a.foo(1) |
32 try: | 65 try: |
33 a.bar() | 66 a.bar() |
34 except NotImplementedError, e: | 67 except NotImplementedError, e: |
35 print e | 68 print e |
36 c.foo(1) | 69 c.foo(1) |
37 a.foo(1) | 70 try: |
71 a.foo(1) | |
72 except NotImplementedError, e: | |
73 print e | |
38 | 74 |
75 ### abc | |
76 print '\nIllustrate `abc` functionality' | |
77 a = AbstractClass() | |
78 e = None | |
79 try: | |
80 a.foo(1) | |
81 # you'd think this would raise an exception since` | |
82 # `AbstractClass.foo` is decorated with `abc.abstractmethod` | |
83 # But see http://docs.python.org/2/library/abc.html : | |
84 # "Using this decorator requires that the class's metaclass | |
85 # is ABCMeta or is derived from it." | |
86 except Exception, e: | |
87 pass | |
88 assert e is None # ! | |
39 | 89 |
90 # as an aside, another good reason not to use super: | |
91 # "Unlike Java abstract methods, these abstract methods may | |
92 # have an implementation. This implementation can be called via the super() | |
93 # mechanism from the class that overrides it. This could be useful as | |
94 # an end-point for a super-call in a framework that uses cooperative | |
95 # multiple-inheritance." | |
96 | |
97 a = Picasso() | |
98 e = None | |
99 try: | |
100 a.foo(1) | |
101 except Exception, e: | |
102 pass | |
103 import pdb; pdb.set_trace() |