Dan Perl wrote:
"jfj" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]

However this is not possible for another instance method:

############
class A:
pass

class B:
def foo(x,y)
    print x,y

b=B()
A.foo = b.foo
a=A()

# error!!!
a.foo()
##############

Python complains that 'foo() takes exactly 2 arguments (1 given)'.
But by calling "b.foo(1)" we prove that it is indeed a function which takes
exactly one argument.


Isn't that inconsistent?


You called b.foo(1) but a.foo(). Note one argument in the first call and no arguments in the second call. Would you have called a.foo(1), you would have gotten the same result as with b.foo(1). I suppose that was just a small omission on your part, but what are you trying to do anyway? It's a very strange use of instance methods.



No omission. If I say:

x=b.foo
x(1)

Then, without looking at the previous code, one can say that "x" is a
function which takes one argument. Continuing with "x":

A.foo = x
# this is ok
A.foo(1)
a=A()
# this is not ok
a.foo()

I expected that when we add this "x" to a class's dictionary and
then we request it from an instance of that class, it will be
converted to an bound-method and receive its --one-- argument
from the referring instance.

So "a.foo()" == "A.foo(a)" == "x(a)" == "b.foo(a)" == "B.foo(b,a)",
or at least "why not?" (head exploded?:)

I'm not trying to do something specific with this though.


G. -- http://mail.python.org/mailman/listinfo/python-list

Reply via email to