Method binding confusion

David MacQuigg dmq at gain.com
Mon May 24 17:48:55 EDT 2004


On Mon, 03 May 2004 19:52:02 +0200, Peter Otten <__peter__ at web.de>
wrote:
<snip>
>The machinery behind the observed behaviour is now somewhat clearer - I
>think you can predict a function's behaviour as a method by checking
>hasattr(func, "__get__"). But still the *reason* (or simply use-case) for
>this dichotomy of functions (to me) remains unclear. Why are not all
>functions created equal?

All functions and methods *should* be unified.  See the section
"Unification of all function forms" in PrototypeSyntax.doc at
http://www.ece.arizona.edu/~edatools/Python

Here is my simplification of the example which started this thread:

import math

def mypow(x, y):
      return x**y

class MathA:
   pow = math.pow

class MathB:
   pow = mypow

ma = MathA()
mb = MathB()

print ma.pow(2,4) #=>
16.0
print mb.pow(2,4) #=>
# TypeError: mypow() takes exactly 2 arguments (3 given)

This problem, and a lot of others that confuse beginners could be
eliminated if all functions and methods had exactly the same sequence
of arguments (no special first argument).

I posted this example in the thread "Unification of Methods and
Functions" 5/22/04, but got only the standard response "You don't need
to do that."  I don't know the origin of this example, so I don't know
if there is a valid requirement or not.  In any case it seems simple
enough that there shouldn't be a problem.

I think the OP is not following this discussion, or even responding to
emails, so if anyone knows if the above is valid code, please let us
know.

-- Dave





More information about the Python-list mailing list