dynamic function add to an instance of a class

R

Richard Lamboj

Hello,

i want to add functions to an instance of a class at runtime. The added
function should contain a default parameter value. The function name and
function default paramter values should be set dynamical.

Kind Regards,

Richi
 
P

Peter Otten

Richard said:
i want to add functions to an instance of a class at runtime. The added
function should contain a default parameter value. The function name and
function default paramter values should be set dynamical.
.... def __init__(self, x):
.... self.x = x
.... def m(self):
.... return self.f(self.x)
........ return self.x + a**b
....True

Confused? The important points are

(1)

functools.partial(f, a1, a2, a3, ...)(b1, b2, b3, ...)

is equivalent to

f(a1, a2, a3, ..., b1, b2, b3, ...)

(2)

If you stick a function into an instance

a.f = f

the call

a.f()

will not automagically pass self as the first argument.

Peter
 
N

News123

Peter said:
... def __init__(self, x):
... self.x = x
... def m(self):
... return self.f(self.x)
...
... return self.x + a**b
...
True

Confused? The important points are

(1)

functools.partial(f, a1, a2, a3, ...)(b1, b2, b3, ...)

is equivalent to

f(a1, a2, a3, ..., b1, b2, b3, ...)

(2)

If you stick a function into an instance

a.f = f


the call

a.f()

will not automagically pass self as the first argument.
The drawback would be, that
b = A(123)
b.f()
would still be called with a as bound object.


N
 
P

Peter Otten

News123 said:
The drawback would be, that
b = A(123)
b.f()
would still be called with a as bound object.

There is no b.f until you explicitly assign it with

b.f = f

If you want the function to work uniformly across all instances of the class
you are better off with adding it to the class

def f(self, x, y):
...

A.f = f

However, if you want x to have a fixed value -- that is beyond the
capabilities of functols partial. You have to wrap f in another function:

A.f = lambda self, y: f(self, 42, y)

Peter
 
R

Richard Lamboj

Am Thursday 29 April 2010 10:13:01 schrieb Peter Otten:
... def __init__(self, x):
... self.x = x
... def m(self):
... return self.f(self.x)
...


... return self.x + a**b
...


True

Confused? The important points are

(1)

functools.partial(f, a1, a2, a3, ...)(b1, b2, b3, ...)

is equivalent to

f(a1, a2, a3, ..., b1, b2, b3, ...)

(2)

If you stick a function into an instance

a.f = f

the call

a.f()

will not automagically pass self as the first argument.

Peter

Thats what i want:

from functools import partial

class A(object):
def add_function(self, function_name, value):
setattr(self, function_name, partial(self.test, value))

def test(self, value):
print value


a = A()
a.add_function("func1", "value1")
a.func1()

Thanks to all for helping me!

Kind Regard,

Richi
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top