~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to Samples_python/abstract_base_class.py

  • Committer: Dave Kuhlman
  • Date: 2015-10-09 19:50:18 UTC
  • mfrom: (35.1.6 Materials)
  • Revision ID: dkuhlman@davekuhlman.org-20151009195018-8rfrdur2zxdjn5ds
Merge after training at Cisco 10/05/2015

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""
 
4
synopsis:
 
5
    Illustrate use of module abc -- abstract base class.
 
6
    This usage pattern controls which classes can and cannot be instantiated.
 
7
"""
 
8
 
 
9
from abc import ABCMeta, abstractmethod
 
10
 
 
11
 
 
12
class A(object):
 
13
    __metaclass__ = ABCMeta
 
14
 
 
15
    @abstractmethod
 
16
    def m1(self):
 
17
        print 'hi'
 
18
 
 
19
 
 
20
class B(A):
 
21
    def m1(self):
 
22
        A.m1(self)
 
23
        print 'bye'
 
24
 
 
25
 
 
26
class C(A):
 
27
    def m2(self):
 
28
        A.m1(self)
 
29
        print 'bye'
 
30
 
 
31
 
 
32
def test():
 
33
    b = B()
 
34
    b.m1()
 
35
    print '-' * 50
 
36
##     c = C()
 
37
##     c.m2()
 
38
    a = A()
 
39
    a.m1()
 
40
 
 
41
 
 
42
test()