Re: Dynamically adding and removing methods

2005-09-29 Thread Ron Adam
Terry Reedy wrote: > "Ron Adam" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>Actually I think I'm getting more confused. At some point the function >>is wrapped. Is it when it's assigned, referenced, or called? > > > When it is referenced via the class. Ok, that's what

Re: Dynamically adding and removing methods

2005-09-28 Thread Steven Bethard
Terry Reedy wrote: > "Ron Adam" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>Actually I think I'm getting more confused. At some point the function >>is wrapped. Is it when it's assigned, referenced, or called? > > > When it is referenced via the class. > If you lookup i

Re: Dynamically adding and removing methods

2005-09-28 Thread Terry Reedy
"Ron Adam" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Actually I think I'm getting more confused. At some point the function > is wrapped. Is it when it's assigned, referenced, or called? When it is referenced via the class. If you lookup in class.__dict__, the function is

Re: Dynamically adding and removing methods

2005-09-28 Thread Ron Adam
Steven D'Aprano wrote: > On Tue, 27 Sep 2005 16:42:21 +, Ron Adam wrote: > > >>>def beacon(self, x): ...print "beacon + %s" % x ... >>> >>> >>>Did you mean bacon? *wink* >> >>Of course... remembering arbitrary word letter sequences is probably my >>worst skill. ;-) Tha

Re: Dynamically adding and removing methods

2005-09-28 Thread Steven D'Aprano
On Tue, 27 Sep 2005 16:42:21 +, Ron Adam wrote: >>> >>> def beacon(self, x): >>>...print "beacon + %s" % x >>>... >> >> >> Did you mean bacon? *wink* > > Of course... remembering arbitrary word letter sequences is probably my > worst skill. ;-) That, and I think for some reason the na

Re: Dynamically adding and removing methods

2005-09-27 Thread Ron Adam
Steven D'Aprano wrote: > On Sun, 25 Sep 2005 14:52:56 +, Ron Adam wrote: > > >>Steven D'Aprano wrote: >> >> >> >>>Or you could put the method in the class and have all instances recognise >>>it: >>> >>>py> C.eggs = new.instancemethod(eggs, None, C) >>>py> C().eggs(3) >>>eggs * 3 >> >>Why not

Re: Dynamically adding and removing methods

2005-09-25 Thread Steven Bethard
Steven D'Aprano wrote: > py> class Klass: > ... pass > ... > py> def eggs(self, x): > ... print "eggs * %s" % x > ... > py> inst = Klass() # Create a class instance. > py> inst.eggs = eggs # Dynamically add a function/method. > py> inst.eggs(1) > Traceback (most recent call last): > Fil

Re: Dynamically adding and removing methods

2005-09-25 Thread Steven D'Aprano
On Sun, 25 Sep 2005 14:52:56 +, Ron Adam wrote: > Steven D'Aprano wrote: > > >> Or you could put the method in the class and have all instances recognise >> it: >> >> py> C.eggs = new.instancemethod(eggs, None, C) >> py> C().eggs(3) >> eggs * 3 > > Why not just add it to the class directly

Re: Dynamically adding and removing methods

2005-09-25 Thread Ron Adam
Steven D'Aprano wrote: > Or you could put the method in the class and have all instances recognise > it: > > py> C.eggs = new.instancemethod(eggs, None, C) > py> C().eggs(3) > eggs * 3 Why not just add it to the class directly? You just have to be sure it's a class and not an instance of a cl

Re: Dynamically adding and removing methods

2005-09-24 Thread Collin Winter
One caveat, as I recently discovered, to dynamically adding methods is that it doesn't work for __foo__ methods. For example, you can't make an object into an iterator by dynamically assigning bound methods to obj.__iter__ and obj.next. Same thing with __getitem__, __setitem__, etc; adding them dir

Dynamically adding and removing methods

2005-09-24 Thread Steven D'Aprano
Suppose I create a class with some methods: py> class C: ... def spam(self, x): ... print "spam " * x ... def ham(self, x): ... print "ham * %s" % x ... py> C().spam(3) spam spam spam >>> C().ham(3) ham * 3 To dynamically remove the methods, delete them from the cl