Confused with methods

jfj jfj at freemail.gr
Sun Feb 6 20:39:40 EST 2005


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?

Thanks,

Gerald.




More information about the Python-list mailing list