Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def merge(seqs):
- print '\n\nCPL[%s]=%s' % (seqs[0][0],seqs),
- res = []; i=0
- while 1:
- nonemptyseqs=[seq for seq in seqs if seq]
- if not nonemptyseqs: return res
- i+=1; print '\n',i,'round: candidates...',
- for seq in nonemptyseqs: # find merge candidates among seq heads
- cand = seq[0]; print ' ',cand,
- nothead=[s for s in nonemptyseqs if cand in s[1:]]
- if nothead: cand=None #reject candidate
- else: break
- if not cand: raise "Inconsistent hierarchy"
- res.append(cand)
- for seq in nonemptyseqs: # remove cand
- if seq[0] == cand: del seq[0]
- def mro(C):
- "Compute the class precedence list (mro) according to C3"
- print "C.__dict__=", C.__dict__
- return merge([[C]]+map(mro,C.__bases__)+[list(C.__bases__)])
- def print_mro(C):
- print '\nMRO[%s]=%s' % (C,mro(C))
- print '\nP22 MRO[%s]=%s' % (C,C.mro())
- class A(object):
- def __init__(self):
- self.a = 1
- def x(self):
- print "A.x"
- def y(self):
- print "A.y"
- def z(self):
- print "A.z"
- class B(A):
- def __init__(self):
- A.__init__(self)
- self.a = 2
- self.b = 3
- def y(self):
- print "B.y"
- def z(self):
- print "B.z"
- class C(object):
- def __init__(self):
- self.a = 4
- self.c = 5
- def y(self):
- print "C.y"
- def z(self):
- print "C.z"
- class D(C, B):
- def __init__(self):
- C.__init__(self)
- B.__init__(self)
- self.d = 6
- def z(self):
- print "D.z"
- # Which __init__ methods are invoked and in which order is determined by the cod
- ing of the individual __init__ methods.
- # When resolving a reference to an attribute of an object that's an instance of
- class D, Python first searches the object's instance variables then uses a simpl
- e left-to-right, depth first search through the class hierarchy. In this case th
- at would mean searching the class C, followed the class B and its superclasses (
- ie, class A, and then any superclasses it may have, et cetera).
- # With the definitions above if we define
- obj = D()
- print "D.__dict__=", D.__dict__
- print_mro(D)
- # then what is printed by each of the following statements?
- print obj.a
- print obj.b
- print obj.c
- print obj.d
- obj.x()
- obj.y()
- obj.z()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement