Dynamically adding methods to objects?

  • Thread starter damian birchler
  • Start date
D

damian birchler

Hello there!

I'm wondering if it is possible to automatically dynamically add
methods to a class instance. For example, if there is a class Foo with
one method named
add_function and an instance of Foo f -

class Foo:
add_function(self, name, args):
# ...
# ...

f = Foo()

-, could one add arbitrary methods to f calling its method
add_function?

I know how to do it manually, I just define a function and assigne it
to f -

def bar():
pass

f.bar = bar -,
but what if this should be done at runtime. How can I get the string
passed to f.add_function as name to be the name of a new function in
the function's definiton?

Thanks a lot
 
?

=?ISO-8859-1?Q?Holger_T=FCrk?=

damian said:
I know how to do it manually, I just define a function and assigne it
to f -

def bar():
pass

f.bar = bar -,
but what if this should be done at runtime. How can I get the string
passed to f.add_function as name to be the name of a new function in
the function's definiton?

f.bar = bar is the same as setattr (f, "bar", bar)

Remember that bar does not become a method of f, but remains
only a function which is stored in f. Is not subject of the
binding of the "self" parameter.

Greetings,

Holger
 
J

John Roth

Holger Türk said:
f.bar = bar is the same as setattr (f, "bar", bar)

Remember that bar does not become a method of f, but remains
only a function which is stored in f. Is not subject of the
binding of the "self" parameter.

Ah, but that's what I want to do - have a ***method***
where the self parameter works the way I expect it to.

John Roth
 
C

Carl Banks

John said:
Ah, but that's what I want to do - have a ***method***
where the self parameter works the way I expect it to.


Is this what you want?

def selfize(obj,func):
def callfunc(*args,**kwargs):
func(obj,*args,**kwargs)
return callfunc

f.bar = withself(f,bar)
 
L

Leif K-Brooks

damian said:
I'm wondering if it is possible to automatically dynamically add
methods to a class instance.

Try something like this:

import new

class Foo(object):
def add_method(self, name, prints):
"""Add a method to the class which prints a string.

Arguments:
name - the name of the method
prints - the string the method should print
"""
def method(self):
print prints

method = new.instancemethod(method, self, Foo)
setattr(self, name, method)

instance = Foo()
instance.add_method('foo', 'I am foo. Fear me!')
instance.add_method('bar', "I am the humble bar. Please don't hurt me.")
instance.foo()
instance.bar()
 
J

Jacek Generowicz

[ f = Foo() ]
Is this what you want?

def selfize(obj,func):
def callfunc(*args,**kwargs):
func(obj,*args,**kwargs)
return callfunc

f.bar = withself(f,bar)

What's "withself" ?

Isn't John saying he wants exactly what the instancemethod function in
the new module was designed for:

import new
f.bar = new.instancemethod(bar, f, Foo)


?
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top