Confused with methods

Dan Perl danperl at rogers.com
Sun Feb 6 11:14:18 EST 2005


"jfj" <jfj at freemail.gr> wrote in message 
news:mailman.2022.1107703890.22381.python-list at python.org...
>I don't understand.
> We can take a function and attach it to an object, and then call it
> as an instance method as long as it has at least one argument:
>
> #############
> class A:
>   pass
>
> def foo(x):
>   print x
>
> A.foo = foo
> a=A()
> a.foo()
> #############
>
> 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. 





More information about the Python-list mailing list