Method binding confusion

A B Carter gnosticray at aol.com
Mon May 3 02:57:26 EDT 2004


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