Dynamic class methods misunderstanding

F. Petitjean littlejohn.75 at news.free.fr
Fri Jan 28 10:41:49 EST 2005


Le Fri, 28 Jan 2005 10:20:30 -0500, Bill Mill a écrit :
> Hello all,
> 
> I have a misunderstanding about dynamic class methods. I don't expect
> this behavior:
> 
> In [2]: class test:
>    ...:      def __init__(self, method):
>    ...:         self.method = method
>    ...:         self.method()
>    ...:
> 
> In [3]: def m(self): print self
>    ...:
> 
> In [4]: test(m)
> ---------------------------------------------------------------------------
> exceptions.TypeError                                 Traceback (most recent call
>  last)
> 
> /cygdrive/c/Documents and Settings/Wmill/<console>
> 
> /cygdrive/c/Documents and Settings/Wmill/<console> in __init__(self, method)
> 
> TypeError: m() takes exactly 1 argument (0 given)
> -----------------------------------------------------------------------------
> 
> Why doesn't m get the implicit self parameter in the self.method()
> call? How would I make it a proper member of the class, so that a
> self.method() call would work with the above "m" function?
The "def m(self):" was not properly indented. So here, "m" is a module level
function, not a method of your class.
> 
> Peace
> Bill Mill
> bill.mill at gmail.com



More information about the Python-list mailing list