callbacks in python

A

Alexandru Mosoi

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

Fredrik Lundh

Alexandru said:
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>
 
A

Alexandru Mosoi

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

Bruno Desthuilliers

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

Bruno Desthuilliers

Bruno Desthuilliers a écrit :
Alexandru Mosoi a écrit :

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

Fredrik Lundh

Bruno said:
from functools import partial

callback = partial(some_func, x=1, y)
callback(z, t=4, u)
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg
 
B

Bruno Desthuilliers

Fredrik Lundh a écrit :
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg

Yeps, I noticed this a couple seconds after posting. Brains needs more
coffe...
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top