How to get parent method as a function object?

David Eppstein eppstein at ics.uci.edu
Wed Dec 11 16:57:59 EST 2002


Ok, we all know
(1) If a class method (with self as argument) includes an expression 
"self.foo", where foo is another method of the same class, the result of 
this expression is some kind of function object with the first argument 
already instantiated, that can then be used anywhere you'd use a normal 
function object.
(2) If you want to call a method from a parent class, that has been 
shadowed in the child, you shouldn't use the self.x syntax, you should 
instead call "parent.method(self, ...)"

So my question is, suppose you don't want to call a shadowed method from 
a parent class, instead you want to create a first-arg-instantiated 
function object like you would get from self.foo?

Other than using lambdas or recursive defs, the only way I can see of 
doing this is to rename the parent method before shadowing it:

class child(parent):
    parentShadowed = parent.shadowed
    def shadowed(self, ...):
        ...
        fun = self.parentShadowed
        ...

Of course, you could use the same renaming trick with any function, you 
don't have to use one of the methods of the parent or even of any class.

Is there a better syntax I'm missing?  Or some other way of accessing 
this pre-instantiation machinery outside of a class?

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list