473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

callbacks in python

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)
Aug 13 '08 #1
6 1984
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>

Aug 13 '08 #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)
First problem is that all unnamed arguments must come BEFORE named ones (Python
limitation). To do what you want define you callback as a class instead.

class Callback(object):
def __init__(x, y):
self.x = x
self.y = y

def __call__(z, t, u):
#
# Do what you want done at every callback
#
callback = Callback(x, y)

callback(z, t, u)

-Larry
Aug 13 '08 #3
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)
Aug 14 '08 #4
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)
Aug 14 '08 #5
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

Aug 14 '08 #6
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...
Aug 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. In the Delphi(5) code: type TCallbackFunc =...
3
by: Paul Moore | last post by:
I'm trying to check a graph (represented in the usual Python adjacency list format) for loops. This is dead simple - you use a depth-first search, and look out for "back edges" (edges from a vertex...
1
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a...
4
by: Randall Hopper | last post by:
What is the correct way to propagate exceptions from Python callbacks? When I do this: Python -> C++ -> Python Callback (example attached) an exception raised in the callback doesn't make it...
5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
4
by: davidstummer | last post by:
I was wondering if anyone could point me to an example. Currently i have a c++ program which calls and c++ dll (i created both). The dll uses SendMessage to pass messages back to the calling .exe,...
3
by: Faisal Alquaddoomi | last post by:
Hello, I'm having a bit of trouble isolating my scripts from each other in my embedded Python interpreter, so that their global namespaces don't get all entangled. I've had some luck with...
1
by: geoffschmidt | last post by:
I'm trying to write an extension in C that delivers callbacks to Python. The C code starts several threads, and I'd like one of the new threads that is started to be able to deliver callbacks to...
3
by: iwl | last post by:
Hello, I would like to add Variables to my embedded python which represents variables from my C++-Programm. I found C-Api-funcs for adding my C-Funcs to python but none to add variables. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.