Re: Dynamic class methods misunderstanding

2005-01-31 Thread Alex Martelli
Christos TZOTZIOY Georgiou <[EMAIL PROTECTED]> wrote: ... > >> > class Test: > >> > def __init__(self, method): > >> > self.m = new.instancemethod(method, self, Test) ... > >self.m = method.__get__(self, Test) > > Almost true; not all builtin functions are descriptors though.

Re: Dynamic class methods misunderstanding

2005-01-31 Thread TZOTZIOY
On Sat, 29 Jan 2005 10:24:27 +0100, rumours say that [EMAIL PROTECTED] (Alex Martelli) might have written: >Bill Mill <[EMAIL PROTECTED]> wrote: > ... >> > class Test: >> > def __init__(self, method): >> > self.m = new.instancemethod(method, self, Test) >> >> Beautiful! thank you ve

Re: Dynamic class methods misunderstanding

2005-01-29 Thread Alex Martelli
Bill Mill <[EMAIL PROTECTED]> wrote: ... > > class Test: > > def __init__(self, method): > > self.m = new.instancemethod(method, self, Test) > > Beautiful! thank you very much. Looking into the "new" module in > python 2.4, that's equivalent to: > > self.m = type(self.__init__)(met

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
On Fri, 28 Jan 2005 14:41:16 -0500, Terry Reedy <[EMAIL PROTECTED]> wrote: > > "Kamilche" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >I see what you're attempting to do. However, your code, if it DID run, > > would result in a method being added to the object, not the object's

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Terry Reedy
"Kamilche" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >I see what you're attempting to do. However, your code, if it DID run, > would result in a method being added to the object, not the object's > class! Modify the class itself, not the object, as follows: > > |class Test: > |

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Craig Ringer
On Fri, 2005-01-28 at 11:17 -0500, Bill Mill wrote: > Beautiful! thank you very much. Looking into the "new" module in > python 2.4, that's equivalent to: > > self.m = type(self.__init__)(method, self, Test) > > I didn't know that you could call types to create another type. Well, a type is ess

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
On Fri, 28 Jan 2005 11:59:50 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > > On Fri, 28 Jan 2005 11:09:16 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > > > > > >>To add m as a new method to the *class*, do this: > >> > >> >>> class test: > >>... def __init__(self, meth

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Hans Nowak
Bill Mill wrote: On Fri, 28 Jan 2005 11:09:16 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > To add m as a new method to the *class*, do this: >>> class test: ... def __init__(self, method): ... self.__class__.method = method ... self.method() ... >>> def m(self): print self ..

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
Kamilche, On Fri, 28 Jan 2005 08:10:07 -0800 (PST), Kamilche <[EMAIL PROTECTED]> wrote: > I see what you're attempting to do. However, your code, if it DID run, > would result in a method being added to the object, not the object's > class! Modify the class itself, not the object, as follows: >

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
Hans, On Fri, 28 Jan 2005 11:09:16 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > > m is a function. When you assign it to self.method, it's still a > function. You don't create a new method that way; all you have is a new > attribute called 'method' containing the function. > I figured as

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
Diez, On Fri, 28 Jan 2005 16:57:37 +0100, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > > > > 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? > >

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Hans Nowak
Bill Mill wrote: 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 ...: [...] Type

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Kamilche
I see what you're attempting to do. However, your code, if it DID run, would result in a method being added to the object, not the object's class! Modify the class itself, not the object, as follows: |class Test: |def __init__(self): |self.method() | |def m(self): |print self | |se

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Diez B. Roggisch
> > 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? Use new.instancemethod: import new class Test: def __init__(self, method): self.m =

Re: Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
On 28 Jan 2005 15:41:49 GMT, F. Petitjean <[EMAIL PROTECTED]> wrote: > 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__(se

Re: Dynamic class methods misunderstanding

2005-01-28 Thread F. Petitjean
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()

Dynamic class methods misunderstanding

2005-01-28 Thread Bill Mill
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) ---