"inherited" keyword in Python?

Paul Foley see at below
Mon Dec 4 21:17:50 EST 2000


On Mon, 4 Dec 2000 16:58:18 +0100, Alex Martelli wrote:

> "Carsten Gaebler" <cg at schlund.de> wrote in message
> news:3A2B9EFA.3A64C13B at schlund.de...
>> Hi there!
>> 
>> Does Python know something like the "inherited" keyword from Borland
>> Pascal? In Pascal it is used to call a method of a class's superclass,
>> i.e. instead of saying "superclass.method()" you say "inherited
>> method()". Can be quite helpful if you change the name of the superclass
>> or just forgot it. :-)

[...]
> Metaprogramming magick was deemed inappropriate here (as I
> read the group consensus).  One *might* have a global function
> called super(), which tried to gauge the appropriate class
> object to use (in single-inheritance cases, only, of course),

Why "of course"?  If you can find out the name of the method which is
calling super() (which can be done, but I don't remember how), and the
class from which it's being called (I can think of ways to avoid
needing this, if it can't be done), it shouldn't be very hard to make
a super() function that works with multiple inheritance.  Use
cpl(class) [see below] to get a properly ordered list of base classes,
and search for the first one that implements the named method.  Call
that.  [You need to know which class the current method belongs to in
order to get the right cpl, otherwise it'll infloop...but that isn't
obtainable, you could maybe have super() maintain a stack to tell it
where it's at, or something...]


def cpl(klass):
    if klass.__dict__.has_key('__cpl__'):
        return klass.__cpl__
    classes = get_heirarchy(klass)
    constraints = []
    for i in map(local_precedence_ordering, classes):
        constraints = constraints + i
    constraints = remove_duplicates(constraints)
    klass.__cpl__ = topological_sort(classes, constraints, cpl_tie_breaker)
    return klass.__cpl__

def topological_sort(elements, constraints, tie_breaker):
    elements = elements[:]
    constraints = constraints[:]
    result = []
    while 1:
        rhs = map(lambda x: x[1], constraints)
        elts = filter(lambda x, rhs=rhs: x not in rhs, elements)
        if not elts:
            if not elements:
                return result
            raise ValueError, "Inconsistent constraints in topological_sort"
        if len(elts) == 1:
            elt = elts[0]
        else:
            elt = tie_breaker(elts, result)
        result.append(elt)
        elements.remove(elt)
        constraints = filter(lambda x, elt=elt: x[0] != elt and x[1] != elt,
                             constraints)

def cpl_tie_breaker(elements, current):
    cpl = current[:]
    cpl.reverse()
    for klass in cpl:
        for i in klass.__bases__:
            if i in elements:
                return i

def local_precedence_ordering(klass):
    if not klass.__bases__:
        return []
    result = []
    classes = [klass] + list(klass.__bases__)
    for i in range(0, len(classes)-1):
        result.append([classes[i], classes[i+1]])
    return result

def get_heirarchy(klass):
    result = [klass]
    for i in map(get_heirarchy, klass.__bases__):
        result = result + i
    return remove_duplicates(result)

def remove_duplicates(list):
    result = []
    for i in list:
        if i not in result:
            result.append(i)
    return result

-- 
C/C++/Java/Perl/etc are for people who want to make things that work.
Common Lisp is for people who want to make things that don't break.
                                                           -- Erik Naggum
(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))



More information about the Python-list mailing list