Advertisement
rdrewd

inherit.py

Mar 25th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. def merge(seqs):
  2.     print '\n\nCPL[%s]=%s' % (seqs[0][0],seqs),
  3.     res = []; i=0
  4.     while 1:
  5.       nonemptyseqs=[seq for seq in seqs if seq]
  6.       if not nonemptyseqs: return res
  7.       i+=1; print '\n',i,'round: candidates...',
  8.       for seq in nonemptyseqs: # find merge candidates among seq heads
  9.           cand = seq[0]; print ' ',cand,
  10.           nothead=[s for s in nonemptyseqs if cand in s[1:]]
  11.           if nothead: cand=None #reject candidate
  12.           else: break
  13.       if not cand: raise "Inconsistent hierarchy"
  14.       res.append(cand)
  15.       for seq in nonemptyseqs: # remove cand
  16.           if seq[0] == cand: del seq[0]
  17.  
  18. def mro(C):
  19.     "Compute the class precedence list (mro) according to C3"
  20.     print "C.__dict__=", C.__dict__
  21.     return merge([[C]]+map(mro,C.__bases__)+[list(C.__bases__)])
  22.  
  23. def print_mro(C):
  24.     print '\nMRO[%s]=%s' % (C,mro(C))
  25.     print '\nP22 MRO[%s]=%s' % (C,C.mro())
  26.  
  27. class A(object):
  28.     def __init__(self):
  29.         self.a = 1
  30.     def x(self):
  31.         print "A.x"
  32.     def y(self):
  33.         print "A.y"
  34.     def z(self):
  35.         print "A.z"
  36.  
  37. class B(A):
  38.     def __init__(self):
  39.         A.__init__(self)
  40.         self.a = 2
  41.         self.b = 3
  42.     def y(self):
  43.         print "B.y"
  44.     def z(self):
  45.         print "B.z"
  46.  
  47. class C(object):
  48.     def __init__(self):
  49.         self.a = 4
  50.         self.c = 5
  51.     def y(self):
  52.         print "C.y"
  53.     def z(self):
  54.         print "C.z"
  55.  
  56. class D(C, B):
  57.     def __init__(self):
  58.         C.__init__(self)
  59.         B.__init__(self)
  60.         self.d = 6
  61.     def z(self):
  62.         print "D.z"
  63.  
  64. # Which __init__ methods are invoked and in which order is determined by the cod
  65. ing of the individual __init__ methods.
  66.  
  67. # When resolving a reference to an attribute of an object that's an instance of
  68. class D, Python first searches the object's instance variables then uses a simpl
  69. e left-to-right, depth first search through the class hierarchy. In this case th
  70. at would mean searching the class C, followed the class B and its superclasses (
  71. ie, class A, and then any superclasses it may have, et cetera).
  72.  
  73. # With the definitions above if we define
  74.  
  75. obj = D()
  76. print "D.__dict__=", D.__dict__
  77. print_mro(D)
  78.  
  79. # then what is printed by each of the following statements?
  80.  
  81. print obj.a
  82.  
  83. print obj.b
  84.  
  85. print obj.c
  86.  
  87. print obj.d
  88.  
  89. obj.x()
  90.  
  91. obj.y()
  92.  
  93. obj.z()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement