Confused with methods

jfj jfj at freemail.gr
Sun Feb 6 23:18:07 EST 2005


Dan Perl wrote:
> "jfj" <jfj at freemail.gr> wrote in message 
> news:mailman.2022.1107703890.22381.python-list at python.org...
>>
>>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.



More information about the Python-list mailing list