dynamic function add to an instance of a class

Discussion in 'Python' started by Richard Lamboj, Apr 29, 2010.

  1. 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
    Richard Lamboj, Apr 29, 2010
    #1
    1. Advertising

  2. Richard Lamboj

    Peter Otten Guest

    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
    Peter Otten, Apr 29, 2010
    #2
    1. Advertising

  3. Richard Lamboj

    News123 Guest

    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
    News123, Apr 29, 2010
    #3
  4. Richard Lamboj

    Peter Otten Guest

    News123 wrote:

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


    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
    Peter Otten, Apr 29, 2010
    #4
  5. Am Thursday 29 April 2010 10:13:01 schrieb Peter Otten:
    > 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


    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
    Richard Lamboj, Apr 29, 2010
    #5
  6. Richard Lamboj

    News123 Guest

    Peter Otten wrote:
    > News123 wrote:


    >>>

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


    you are sooo right.
    My fault.
    News123, Apr 29, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,373
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Gerry Sutton
    Replies:
    1
    Views:
    523
    Peter Otten
    Apr 16, 2005
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    356
    Martin P. Hellwig
    Mar 26, 2010
  4. Eustaquio Rangel de Oliveira Jr.
    Replies:
    4
    Views:
    139
    Robert Klemme
    Jul 5, 2005
  5. Luc Juggery
    Replies:
    3
    Views:
    84
    Luc Juggery
    Nov 4, 2006
Loading...

Share This Page