[Python-bugs-list] redefined class methods don't get passed a class instance (PR#212)

guido@python.org guido@python.org
Fri, 25 Feb 2000 06:35:35 -0500 (EST)


> Here, I'm trying to redefine a method by assigning to it a new function
> definition.  It has the same parameters as the original definition, but when it
> gets called after the assignment, I get "TypeError: not enough arguments;
> expected 1, got 0"  Isn't the class instance supposed to be passed?
> 
> class A:
>  def g(self):
>   print self.h()
>  def h(self):
>   return "hi"
> 
> a1 = A()
> a1.g()
> 
> a1.h = lambda self : "bye"
> a1.g()

You can't do it this way; if you assign a function directly to an
attribute of the instance it won't get self passed.  You can assign a
function to the class (A.h = lambda...) and then it *will* get self
passed.

Note that you abused the bugs list for a language question; if you
have more questions, please write to help@python.org or post to
comp.lang.python.

--Guido van Rossum (home page: http://www.python.org/~guido/)