How to get parent method as a function object?

Steve Holden sholden at holdenweb.com
Wed Dec 11 18:47:57 EST 2002


"David Eppstein" <eppstein at ics.uci.edu> wrote ...
> Ok, we all know

[I'm translating a bit first, since I find your terminology confusing]

> (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.

Call it a bound method, which is its name :-)

> (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, ...)"
>
By "shadowed" I presume you mean overridden (i.e. redefined in the namespace
of the subclass)? That's what creates the need for the unbound method syntax
parent.method(self, ...). The method name as an attribute of self must refer
to the subtype's method.

> 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?
>
You mean you want to bind an instance of the subclass to a method of the
superclass?

> 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.
>
I presume this is tested? Let's see ...

>>> class parent(object):
...    def mymethod(self, arg):
...       print "Parent mymethod, instance", self, arg
...
>>> class child(parent):
...    parentMyMethod = parent.mymethod
...    def mymethod(self, arg):
...       print "Child mymethod, instance", self, arg
...       self.parentMyMethod(234)>>> tst = child()
...
>>> tst.mymethod(123)
Child mymethod, instance <__main__.child object at 0x1011dd80> 123
Parent mymethod, instance <__main__.child object at 0x1011dd80> 234
>>>
OK, so that seems to work and I now think I understand what you want to do.

> Is there a better syntax I'm missing?  Or some other way of accessing
> this pre-instantiation machinery outside of a class?
>
Well I certainly wouldn't go so far as to say "no", but offhand I can't
think of one. Is there some way one might use "super()", I wonder...

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list