Adding methods to instances

E

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 example:

xx = """def dynamic(self):
print "dynamic", self.testAtt
"""

class Test(object):
testAtt = "sample"
def normalMethod(self):
print "normal", self.testAtt

testInstance = Test()
# Here's where the magic is needed
# so that the rest of this example works.
testInstance.normal()
-> 'normal', 'sample'
testInstance.dynamic()
-> 'dynamic', 'sample'

So? Am I nuts? Or is this possible?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
 
L

Lawrence Oluyede

Il 2005-12-15 said:
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 example:

xx = """def dynamic(self):
print "dynamic", self.testAtt
"""

class Test(object):
testAtt = "sample"
def normalMethod(self):
print "normal", self.testAtt

testInstance = Test()
# Here's where the magic is needed
# so that the rest of this example works.
testInstance.normal()
-> 'normal', 'sample'
testInstance.dynamic()
-> 'dynamic', 'sample'

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
"""

exec xx

class Test(object):
testAtt = "sample"
def normalMethod(self):
print "normal", self.testAtt

t = Test()
Test.dynamic = dynamic
t.dynamic()
 
E

Ed Leafe

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
"""

exec xx

class Test(object):
testAtt = "sample"
def normalMethod(self):
print "normal", self.testAtt

t = Test()
Test.dynamic = dynamic
t.dynamic()

Thanks! I knew I had done this before. My mistake was that I was
setting the exec'd code to the instance, and not to the class. That
works, but makes it a function, which doesn't automatically get sent
the 'self' reference.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
 
A

Antoon Pardon

Op 2005-12-15 said:
Thanks! I knew I had done this before. My mistake was that I was
setting the exec'd code to the instance, and not to the class. That
works, but makes it a function, which doesn't automatically get sent
the 'self' reference.

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.
 
B

Ben Hutchings

Antoon Pardon said:
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.

How about:

def bind(fun, arg):
return lambda *a, **k: fun(arg, *a, **k)

t.dynamic = bind(dynamic, t)
 
D

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()
 
E

Ed Leafe

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 - I hadn't noticed that, since all my tests
had single instances.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top