Re: Adding methods to instances

2005-12-16 Thread Ed Leafe
On Dec 16, 2005, at 6:26 PM, [EMAIL PROTECTED] wrote: > You can also just use: > > t.dynamic = dynamic.__get__(t) OK, I've tried that, and it does indeed work well. I've never had occasion to use descriptors before, and while it's clear what's going on, it still has a 'magical' feel to i

Re: Adding methods to instances

2005-12-16 Thread Ed Leafe
On Dec 16, 2005, at 7:55 AM, Antoon Pardon wrote: > But this will make the function a method to all instances of the > class. > Is that what you want? From your first post I had the impression you > only wanted the function to be the method of one particular instance. Yes, you're correct -

Re: Adding methods to instances

2005-12-16 Thread ziga . seilnacht
You can also just use: t.dynamic = dynamic.__get__(t) -- http://mail.python.org/mailman/listinfo/python-list

Re: Adding methods to instances

2005-12-16 Thread Ben Hutchings
Antoon Pardon <[EMAIL PROTECTED]> wrote: > Op 2005-12-15, Ed Leafe schreef <[EMAIL PROTECTED]>: >> On Dec 15, 2005, at 11:51 AM, Lawrence Oluyede wrote: >> So? Am I nuts? Or is this possible? >>> >>> Yes it is, use exec() to turn your string in valid Python code >>> and bind the dynamic f

Re: Adding methods to instances

2005-12-16 Thread Dody Suria Wijaya
To avoid that: - subclass Test first class SubclassTest(T): pass - assign the method to SubclassTest's attribute, SubclassTest.dynamic = dynamic - then assign the new class to magic variable __class__ : t.__class__ = SubclassTest t.dynamic() Antoon Pardon wrote: > But this will make the

Re: Adding methods to instances

2005-12-16 Thread Antoon Pardon
Op 2005-12-15, Ed Leafe schreef <[EMAIL PROTECTED]>: > On Dec 15, 2005, at 11:51 AM, Lawrence Oluyede wrote: > >>> So? Am I nuts? Or is this possible? >> >> Yes it is, use exec() to turn your string in valid Python code >> and bind the dynamic function as a method of class Test >> >> xx = """d

Re: Adding methods to instances

2005-12-15 Thread Ed Leafe
On Dec 15, 2005, at 11:51 AM, Lawrence Oluyede wrote: >> So? Am I nuts? Or is this possible? > > Yes it is, use exec() to turn your string in valid Python code > and bind the dynamic function as a method of class Test > > xx = """def dynamic(self): > print "dynamic", self.testAtt > """ >

Re: Adding methods to instances

2005-12-15 Thread Lawrence Oluyede
Il 2005-12-15, Ed Leafe <[EMAIL PROTECTED]> ha scritto: > Here's what I'm trying to do; please let me know if I'm nuts or > not. > > Given a string consisting of the code you would normally define > in a class's method, add that compiled code to an instance of that > class so that i

Adding methods to instances

2005-12-15 Thread Ed Leafe
Here's what I'm trying to do; please let me know if I'm nuts or not. Given a string consisting of the code you would normally define in a class's method, add that compiled code to an instance of that class so that it can be called just like any other bound method. Here's an examp