Method binding confusion

David MacQuigg dmq at gain.com
Tue May 25 23:31:47 EDT 2004


On 25 May 2004 18:11:02 -0700, myles at geocities.com (Myles) wrote:

>> extra argument, should be sufficient clue to add the staticmethod
>> wrapper.
>
>I'm coming from a dabbler's viewpoint.
>Why the binding tricks ? Couldn't you just "wrap" the methods ?
>My solution:
>----
>import math
>
>def mypow(x, y):
>    return x**y
>
>class MathA:
>    def pow(self, x, y):
>        return mypow(x,y)
>
>class MathB:
>    def pow(self, x, y):
>        return math.pow(x,y)
>
>ma = MathA()
>mb = MathB()
>
>print ma.pow(2, 3)
>print mb.pow(2, 3)
>----
>This seems more obvious and consistent to me, or am I missing the
>point of the discussion ?

This is similar to what the original poster had, before I stripped it
down to the smallest example that would show the error.  I'm sure
there are other solutions as well, including: Don't do this.

What makes this problem interesting to me, is that you can actually
fix a problem as tricky as this without really understanding the
mechanism behind it.  I have seen that error message many times.  At
first it puzzled me, because there were clearly two arguments, not
three, in the original call.  After seeing it a few times, I now
recognize that the extra argument is coming from Python's method of
passing 'self'.  'self' is not needed in this case, so we wrap the
troublesome function in staticmethod( ), and the problem goes away.

-- Dave




More information about the Python-list mailing list