[Python-Dev] Re: my proposals about mros (was: perplexed by mro)

Samuele Pedroni pedronis@bluewin.ch
Fri, 4 Oct 2002 19:31:54 +0200


[me]
> A bit of polishing and I will post it.
>

def pmerge(seqs):
    res = []
    while 1:
      for seq in seqs:
          if seq: break
      else: # all sequences are empty
          return res
      cands = []
      for seq in seqs: # find merge candidates among seq heads
          if seq:
              cand = seq[0]
              for seq in seqs:
                  if cand in seq[1:]: break
              else: # cand is not in any sequences' tail => constraint-free
                  cands.append(cand)
      if not cands:
          raise "inconsistent precedences"
      next = cands[0]
      # append next to result and remove it from sequences
      res.append(next)
      for seq in seqs:
          if seq and seq[0] == next:
              del seq[0]

def c3_cpl(cl): # compute class precedence list (mro) of class cl according to
C3
    inputs = [[cl]]
    for base in cl.__bases__:
        cpl = c3_cpl(base)
        inputs.append(cpl)
    inputs.append(list(cl.__bases__)) # C3: considers directly also inheritance
list of cl
    return pmerge(inputs)