callbacks in python

Discussion in 'Python' started by Alexandru Mosoi, Aug 13, 2008.

  1. does anyone know a nice implementation of callbacks in python? i have
    issues mixing named & unamed parameters. i want build a callback over
    a function such that some parameters are passed when callback is
    created and the rest are passed when the function is called.

    example:
    callback = Callback(function, x=1, y)
    callback(z, t=4, u)
     
    Alexandru Mosoi, Aug 13, 2008
    #1
    1. Advertising

  2. Alexandru Mosoi wrote:

    > does anyone know a nice implementation of callbacks in python? i have
    > issues mixing named & unamed parameters. i want build a callback over
    > a function such that some parameters are passed when callback is
    > created and the rest are passed when the function is called.
    >
    > example:
    > callback = Callback(function, x=1, y)
    > callback(z, t=4, u)


    your use of the word "callback" is a bit unusual, and your example isn't
    valid Python code, but it looks as if functools.partial might be what
    you need:

    http://docs.python.org/lib/module-functools.html

    </F>
     
    Fredrik Lundh, Aug 13, 2008
    #2
    1. Advertising

  3. On Aug 14, 12:02 am, Fredrik Lundh <> wrote:
    > your use of the word "callback" is a bit unusual, and your example isn't
    > valid Python code, but it looks as if functools.partial might be what
    > you need:
    >
    >      http://docs.python.org/lib/module-functools.html


    my current implementation is very similar to partial() :) (10x, i'll
    use partial from now on). however it seems that I don't understand
    very well positional and keyword arguments in python because I got the
    error described here: http://docs.python.org/ref/calls.html#calls
    (TypeError: f() got multiple values for keyword argument 'a') which
    confused me even more. so if you pass positional and keyword arguments
    to both partial() and function returned the order of passed arguments
    might be different than expected. i was looking for an implementation
    that somehow avoids this confusion.
     
    Alexandru Mosoi, Aug 13, 2008
    #3
  4. Alexandru Mosoi a écrit :
    > does anyone know a nice implementation of callbacks in python? i have
    > issues mixing named & unamed parameters. i want build a callback over
    > a function such that some parameters are passed when callback is
    > created and the rest are passed when the function is called.
    >
    > example:
    > callback = Callback(function, x=1, y)
    > callback(z, t=4, u)


    from functools import partial

    callback = partial(some_func, x=1, y)
    callback(z, t=4, u)
     
    Bruno Desthuilliers, Aug 14, 2008
    #4
  5. Bruno Desthuilliers a écrit :
    > Alexandru Mosoi a écrit :
    >> does anyone know a nice implementation of callbacks in python? i have
    >> issues mixing named & unamed parameters. i want build a callback over
    >> a function such that some parameters are passed when callback is
    >> created and the rest are passed when the function is called.
    >>
    >> example:
    >> callback = Callback(function, x=1, y)
    >> callback(z, t=4, u)

    >
    > from functools import partial
    >
    > callback = partial(some_func, x=1, y)
    > callback(z, t=4, u)


    Stupid copy-paste :(

    callback = partial(some_func, y, x=1)
    callback(z, u, t=4)
     
    Bruno Desthuilliers, Aug 14, 2008
    #5
  6. Bruno Desthuilliers wrote:

    > from functools import partial
    >
    > callback = partial(some_func, x=1, y)
    > callback(z, t=4, u)


    >>> from functools import partial
    >>> callback = partial(some_func, x=1, y)

    File "<stdin>", line 1
    SyntaxError: non-keyword arg after keyword arg
     
    Fredrik Lundh, Aug 14, 2008
    #6
  7. Fredrik Lundh a écrit :
    > Bruno Desthuilliers wrote:
    >
    >> from functools import partial
    >>
    >> callback = partial(some_func, x=1, y)
    >> callback(z, t=4, u)

    >
    > >>> from functools import partial
    > >>> callback = partial(some_func, x=1, y)

    > File "<stdin>", line 1
    > SyntaxError: non-keyword arg after keyword arg
    >


    Yeps, I noticed this a couple seconds after posting. Brains needs more
    coffe...
     
    Bruno Desthuilliers, Aug 14, 2008
    #7
    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. Mikko Ohtamaa
    Replies:
    2
    Views:
    428
    John J. Lee
    Jul 4, 2003
  2. Melissa Wallis

    omniorb, python and callbacks

    Melissa Wallis, Apr 26, 2004, in forum: Python
    Replies:
    1
    Views:
    428
    Duncan Grisby
    Apr 28, 2004
  3. Marco Aschwanden

    Python, DDE and Callbacks

    Marco Aschwanden, May 21, 2004, in forum: Python
    Replies:
    0
    Views:
    5,058
    Marco Aschwanden
    May 21, 2004
  4. Randall Hopper

    Python callbacks & PyGILState_Release()

    Randall Hopper, Apr 23, 2005, in forum: Python
    Replies:
    4
    Views:
    509
    Randall Hopper
    Apr 26, 2005
  5. Replies:
    4
    Views:
    1,040
Loading...

Share This Page