Question on Bound & unbound methods

Erik Max Francis max at alcyone.com
Fri May 16 04:28:01 EDT 2003


K G wrote:

> Now even though "afunc" features in Base's __dict__, am not able
> to call the function. Even though am able to acquire a handle to
> afunc am helpless. I wonder if it makes sense to have such a
> function
> defined in a class. I guess we have to use staticmethod() in case
> we want to define a static method?? bcos that is the effect i was
> lookign for when i defined afunc().

In Python, the "self" argument to methods is not implicit, it must be
explicitly declared.  (Its name need not be literally `self', but it's
generally unwise and confusing to name it something else.)

In this case, if what you wanted was a static method, you should indeed
use staticmethod:

	class C:
	    def f(): ...
	    f = staticmethod(f)

> Coming to bound & unbound methods,

If you know C++, unbound methods are like C++ member function pointers. 
There is no analog to bound methods in C++, except for the legwork that
has to be done manually, usually via functors.

The difference is simply that one is already tied to an instance (the
bound method) whereas the other is not (unbound).  When you reference a
method attribute of an instance object, you magically get a bound method
(thank you, Python).  If you reference a method attribute of a _class_
object, you get the unbound method.

The difference is that with a bound method, the instance is already
implied and you need not specify it.  With unbound methods, you must
supply it explicitly as the first argument.

If C is a class and c is an instance, C defines a method m, then c.m is
a bound method, whereas C.m is unbound.  These calls would be
equivalent:

	c.m(...)
	C.m(c, ...)

> 2. The same method is supposed to be "bound" if it's called using
> an instance and unbound if called using the class object except
> that instance needs to be passed explicitly in the latter case.

Right.

> Now when w'd i use a unbound method?
> 
> 1. If i have to call super class's methods
> 
> class Derived(Base):
>     def func(self):
>        Base.func(self)

That's an awfully good example.

> 2. If we have a instance variable and instance method name to be
> the same, then the only way am able to access the method is by
> using unbound methods.

This is true in your case but unwise.  Python really handles any
attribute lookup by looking at the attributes in the instance, then
those in the base class(es), then those in _their_ base class(es), and
so on.  So having an attribute (method or not) in the class and then
overriding that with an attribute in the instance to have a completely
different contextual meaning (as you're doing here) is likely to result
in more confusion than it is worth.

> Are there any other situations where unbound methods are
> applicable?

You first case -- calling the base class' method from within the derived
class method (and presumably adding additional functionality) is a prime
example of where unbound methods are used.  Bound methods are used
routinely and are the usual way of exploiting methods.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ But when I reached the finished line / Young black male
\__/  Ice Cube




More information about the Python-list mailing list