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) 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>
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
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 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 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 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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 =...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |