Re: Adding method to a class on the fly

2007-06-24 Thread John Henry
On Jun 24, 12:40 pm, John Henry <[EMAIL PROTECTED]> wrote: > On Jun 24, 1:19 am, John Henry <[EMAIL PROTECTED]> wrote: > > > > > On Jun 23, 6:24 pm, Steven D'Aprano > > > <[EMAIL PROTECTED]> wrote: > > > On Sat, 23 Jun 2007 12:31:39 -0700, John Henry wrote: > > > > it works fine but PythonCard isn'

Re: Adding method to a class on the fly

2007-06-24 Thread John Henry
On Jun 24, 1:19 am, John Henry <[EMAIL PROTECTED]> wrote: > On Jun 23, 6:24 pm, Steven D'Aprano > > <[EMAIL PROTECTED]> wrote: > > On Sat, 23 Jun 2007 12:31:39 -0700, John Henry wrote: > > > it works fine but PythonCard isn't calling this function when I > > > clicked on the button. > > > I think y

Re: Adding method to a class on the fly

2007-06-24 Thread John Henry
On Jun 23, 6:24 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 23 Jun 2007 12:31:39 -0700, John Henry wrote: > > it works fine but PythonCard isn't calling this function when I > > clicked on the button. > > I think you need to take this question onto a PythonCard list. I have no > idea h

Re: Adding method to a class on the fly

2007-06-23 Thread Steven D'Aprano
On Sat, 23 Jun 2007 12:31:39 -0700, John Henry wrote: > it works fine but PythonCard isn't calling this function when I > clicked on the button. I think you need to take this question onto a PythonCard list. I have no idea how PythonCard decides which method to call. -- Steven. -- http://ma

Re: Adding method to a class on the fly

2007-06-23 Thread John Henry
On Jun 23, 10:56 am, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 23 Jun 2007 09:06:36 -0700, John Henry wrote: > > >> > But then how do I create the on_Button1_mouseClick function? > > >> That depends on what it is supposed to do, but in general you want a > >> factory function -- a functi

Re: Adding method to a class on the fly

2007-06-23 Thread John Henry
On Jun 23, 10:56 am, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 23 Jun 2007 09:06:36 -0700, John Henry wrote: > > >> > But then how do I create the on_Button1_mouseClick function? > > >> That depends on what it is supposed to do, but in general you want a > >> factory function -- a functi

Re: Adding method to a class on the fly

2007-06-23 Thread Steven D'Aprano
On Sat, 23 Jun 2007 09:06:36 -0700, John Henry wrote: >> >> > But then how do I create the on_Button1_mouseClick function? >> >> That depends on what it is supposed to do, but in general you want a >> factory function -- a function that returns functions. Here's a simple >> example: >> > > > Ste

Re: Adding method to a class on the fly

2007-06-23 Thread John Henry
> > > But then how do I create the on_Button1_mouseClick function? > > That depends on what it is supposed to do, but in general you want a > factory function -- a function that returns functions. Here's a simple > example: > Steven, May be I didn't explain it clearly: the PythonCard package exp

Re: Adding method to a class on the fly

2007-06-23 Thread Steven D'Aprano
On Sat, 23 Jun 2007 00:02:09 -0700, John Henry wrote: [snip] > Notice that the event handler for mouseClick to Button1 is done via > the function on_Button1_mouseClick. This is very simple and works > great - until you try to create the button on the fly. > > Creating the button itself is no pro

Re: Adding method to a class on the fly

2007-06-23 Thread John Henry
On Jun 22, 7:36 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Fri, 22 Jun 2007 14:44:54 -0700, John Henry wrote: > > The above doesn't exactly do I what need. I was looking for a way to > > add method to a class at run time. > > > What does work, is to define an entire sub-class at run time.

Re: Adding method to a class on the fly

2007-06-22 Thread Steven D'Aprano
On Fri, 22 Jun 2007 14:44:54 -0700, John Henry wrote: > The above doesn't exactly do I what need. I was looking for a way to > add method to a class at run time. > > What does work, is to define an entire sub-class at run time. Like: > > class DummyParent: > def __init__(self): > r

Re: Adding method to a class on the fly

2007-06-22 Thread James Stroud
John Henry wrote: > Hi list, > > I have a need to create class methods on the fly. For example, if I > do: > > class Dummy: > def __init__(self): > exec '''def method_dynamic(self):\n\treturn > self.method_static("it's me")''' > return > > def method_static(self, text):

Re: Adding method to a class on the fly

2007-06-22 Thread James Stroud
7stud wrote: > On Jun 22, 3:23 pm, askel <[EMAIL PROTECTED]> wrote: > >>sorry, of course last line should be: >>Dummy().method2('Hello, world!') > > > ..which doesn't meet the op's requirements. > Which were contradictory. -- http://mail.python.org/mailman/listinfo/python-list

Re: Adding method to a class on the fly

2007-06-22 Thread attn . steven . kuo
On Jun 22, 2:44 pm, John Henry <[EMAIL PROTECTED]> wrote: > On Jun 22, 2:28 pm, askel <[EMAIL PROTECTED]> wrote: > (snipped) > > The above doesn't exactly do I what need. I was looking for a way to > add method to a class at run time. I'm not sure what you mean by this. Bind an attribute -- a

Re: Adding method to a class on the fly

2007-06-22 Thread John Henry
On Jun 22, 2:28 pm, askel <[EMAIL PROTECTED]> wrote: > On Jun 22, 5:17 pm, 7stud <[EMAIL PROTECTED]> wrote: > > > > > On Jun 22, 2:24 pm, askel <[EMAIL PROTECTED]> wrote: > > > > class Dummy: > > > def method(self, arg): > > > print arg > > > > def method2(self, arg): > > > self.method(arg)

Re: Adding method to a class on the fly

2007-06-22 Thread askel
On Jun 22, 5:17 pm, 7stud <[EMAIL PROTECTED]> wrote: > On Jun 22, 2:24 pm, askel <[EMAIL PROTECTED]> wrote: > > > class Dummy: > > def method(self, arg): > > print arg > > > def method2(self, arg): > > self.method(arg) > > > Dummy.method2 = method2 > > Dummy.method2('Hello, world!') > > Tra

Re: Adding method to a class on the fly

2007-06-22 Thread 7stud
On Jun 22, 3:23 pm, askel <[EMAIL PROTECTED]> wrote: > sorry, of course last line should be: > Dummy().method2('Hello, world!') ..which doesn't meet the op's requirements. -- http://mail.python.org/mailman/listinfo/python-list

Re: Adding method to a class on the fly

2007-06-22 Thread askel
On Jun 22, 5:17 pm, 7stud <[EMAIL PROTECTED]> wrote: > On Jun 22, 2:24 pm, askel <[EMAIL PROTECTED]> wrote: > > > class Dummy: > > def method(self, arg): > > print arg > > > def method2(self, arg): > > self.method(arg) > > > Dummy.method2 = method2 > > Dummy.method2('Hello, world!') > > Tra

Re: Adding method to a class on the fly

2007-06-22 Thread 7stud
On Jun 22, 2:24 pm, askel <[EMAIL PROTECTED]> wrote: > class Dummy: > def method(self, arg): > print arg > > def method2(self, arg): > self.method(arg) > > Dummy.method2 = method2 > Dummy.method2('Hello, world!') Traceback (most recent call last): File "test1.py", line 8, in ? Dummy.

Re: Adding method to a class on the fly

2007-06-22 Thread askel
On Jun 22, 3:02 pm, John Henry <[EMAIL PROTECTED]> wrote: > Hi list, > > I have a need to create class methods on the fly. For example, if I > do: > > class Dummy: > def __init__(self): > exec '''def method_dynamic(self):\n\treturn > self.method_static("it's me")''' > return >

Re: Adding method to a class on the fly

2007-06-22 Thread John Henry
Found a message on the web that says I need to use setattr to add the method to the class at run time. But how do I do that? Regards, On Jun 22, 12:02 pm, John Henry <[EMAIL PROTECTED]> wrote: > Hi list, > > I have a need to create class methods on the fly. For example, if I > do: > > class Dum

Adding method to a class on the fly

2007-06-22 Thread John Henry
Hi list, I have a need to create class methods on the fly. For example, if I do: class Dummy: def __init__(self): exec '''def method_dynamic(self):\n\treturn self.method_static("it's me")''' return def method_static(self, text): print text return I like