Method binding confusion

Sridhar R sridharinfinity at yahoo.com
Mon May 3 07:06:05 EDT 2004


Bound and Unbound methods are different and server different purposes.
 See documentation for more details.

gnosticray at aol.com (A B Carter) wrote in message news:<ca6d23f8.0405022257.132f03cc at posting.google.com>...
> I'm a bit confused by the behavior of the following code:
> 
> 
> import math
> import myModule
> 
> class Klass(object):
>    def doOperation(self, x, y):
>       return self.operator(x, y)
> 
> class KlassMath(Klass):
>    operator = math.pow
> 
> class KlassMyModule(Klass):
>    operator = myModule.pow
> 
> km = KlassMath()
> kmy = KlassMyModule()
> 
> km.doOperation(2,4)
> km.doOperation(2,4)
> 
> 
> The last call fails with "TypeError: takes exactly 2 argumetns (3
> given)" I understand that in KlassMyModule the operator is being
> treated as a method and a reference to the class instance is being
> past as the first argument. And I also understand that Python is
> treating a function from the standard math libary differently from a
> function I defined in my own module. What I don't understand is why.
> 
> A B Carter



More information about the Python-list mailing list