From: Richard Lamboj on 29 Apr 2010 03:55 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
From: James Mills on 29 Apr 2010 04:12 On Thu, Apr 29, 2010 at 5:55 PM, Richard Lamboj <richard.lamboj(a)bilcom.at> wrote: > 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. The normal way of doing this by binding a new MethodType to an instance: >>> class A(object): .... def foo(self): .... return "foo" .... >>> def bar(self): .... return "bar" .... >>> a = A() >>> a.foo() 'foo' >>> a.bar() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'A' object has no attribute 'bar' >>> from types import MethodType >>> setattr(a, "bar", MethodType(bar, a)) >>> a.bar() 'bar' >>> a.foo <bound method A.foo of <__main__.A object at 0x8380e8c>> >>> a.bar <bound method ?.bar of <__main__.A object at 0x8380e8c>> >>> cheers James
From: Peter Otten on 29 Apr 2010 04:13 Richard Lamboj wrote: > 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. >>> class A(object): .... def __init__(self, x): .... self.x = x .... def m(self): .... return self.f(self.x) .... >>> a = A(42) >>> >>> def foo(self, a, b): .... return self.x + a**b .... >>> from functools import partial >>> a.f = partial(foo, a, 3) >>> a.m() 109418989131512359251L >>> 42 + 3**42 == _ 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
From: Richard Lamboj on 29 Apr 2010 04:15 Am Thursday 29 April 2010 09:59:22 schrieb Xavier Ho: > On Thu, Apr 29, 2010 at 5:55 PM, Richard Lamboj <richard.lamboj(a)bilcom.at>wrote: > > 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 > > <http://mail.python.org/mailman/listinfo/python-list> > > What did you mean by "The function name and function default paramter > values should be set dynamical." ? Also, have you tried anything we can > see? > > Cheers, > Xav No i don't have any sample code. I just want to add new functions to an class instance at runtime and i also want to set the default parameter values of this new function. Dynamical: Class Test(object): def add_function(self, function_name="new_function", param="new_param", value="new_value"): ... or something like: Class Test(object): def add_function(self, function_name="new_function", parameters=[["param1", "value1"],["param2", "value2"]]): ... Statical created it would look like: Class Test(object): def new_function1(new_parm1="new_value1"): pass def new_function2(new_parm2="new_value2"): pass def new_function3(new_parm3="new_value3"): pass Kind Regards, Richi
From: News123 on 29 Apr 2010 04:31 Peter Otten wrote: > Richard Lamboj wrote: > >> 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. > >>>> class A(object): > ... def __init__(self, x): > ... self.x = x > ... def m(self): > ... return self.f(self.x) > ... >>>> a = A(42) >>>> >>>> def foo(self, a, b): > ... return self.x + a**b > ... >>>> from functools import partial >>>> a.f = partial(foo, a, 3) >>>> a.m() > 109418989131512359251L >>>> 42 + 3**42 == _ > 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
|
Next
|
Last
Pages: 1 2 Prev: help req installing python 2.6 Next: matching strings in a large set of strings |