473,395 Members | 1,624 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,395 software developers and data experts.

How do I set a callback in Python?

I can't for the life of me figure out how to set a callback in
Python. I have a class, which wraps another class. The second class
needs a callback assigned. I don't want to use globals for it.
Here's what I'd like to do:

class MyWrapper:
def get_login(self, username):
return self.user, self.pass

def __init__(self, user, pass):
self.user = user
self.pass = pass

self.client = Client("connection string")
self.client.callback_login = get_login

.... but obviously, the Client class, when it calls the callback,
doesn't pass a reference to the "self" object. How do I do this?

-- Chris
Sep 8 '08 #1
3 1531
catsclaw wrote in news:d797403a-e492-403f-933a-bd18ef53d5c0
@k13g2000hse.googlegroups.com in comp.lang.python:
I can't for the life of me figure out how to set a callback in
Python. I have a class, which wraps another class. The second class
needs a callback assigned. I don't want to use globals for it.
Here's what I'd like to do:

class MyWrapper:
def get_login(self, username):
return self.user, self.pass

def __init__(self, user, pass):
self.user = user
self.pass = pass

self.client = Client("connection string")
self.client.callback_login = get_login

... but obviously, the Client class, when it calls the callback,
doesn't pass a reference to the "self" object. How do I do this?

use:
self.client.callback_login = self.get_login
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sep 8 '08 #2
catsclaw schrieb:
I can't for the life of me figure out how to set a callback in
Python. I have a class, which wraps another class. The second class
needs a callback assigned. I don't want to use globals for it.
Here's what I'd like to do:

class MyWrapper:
def get_login(self, username):
return self.user, self.pass

def __init__(self, user, pass):
self.user = user
self.pass = pass

self.client = Client("connection string")
self.client.callback_login = get_login

... but obviously, the Client class, when it calls the callback,
doesn't pass a reference to the "self" object. How do I do this?

Do self.get_login. The difference is that this creates a so-called
"bound method". Google for that, and play around in the interpreter with
an object and references to it's methods, either through the class or
the instance to see the difference.

Diez
Sep 8 '08 #3
catsclaw wrote:
I can't for the life of me figure out how to set a callback in
Python. I have a class, which wraps another class. The second class
needs a callback assigned. I don't want to use globals for it.
Here's what I'd like to do:

class MyWrapper:
def get_login(self, username):
return self.user, self.pass

def __init__(self, user, pass):
self.user = user
self.pass = pass

self.client = Client("connection string")
self.client.callback_login = get_login
Make that list line:

self.client.callback_login = self.get_login
Then you are passing a "bound" method (meaning bound to a particular instance).
The client class calls its callback_login as a normal function with parameters,
and the Wrapper get_login will be called on the proper instance of MyWrapper.

This is a very common thing for many uses, including callbacks.
Hoping I understood what you wanted correctly,
Gary Herron

... but obviously, the Client class, when it calls the callback,
doesn't pass a reference to the "self" object. How do I do this?

-- Chris
--
http://mail.python.org/mailman/listinfo/python-list
Sep 8 '08 #4

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

Similar topics

2
by: Pierre Rouleau | last post by:
I have a GUI application written in Python (with WxPython) which uses some low level classes also written in Python. These low level Python classes are given a pointer to some GUI Python object. ...
1
by: Markus von Ehr | last post by:
Hi, I tried to build the swig: callback example, but when I run the runme.py file I get a message box: abnormal program termination The error appears in already in the first line of the...
6
by: Marian Aldenhövel | last post by:
Hi, I am using the FMOD audio-library with the pyFMOD python bindings. pyFMOD uses ctypes. It is possible to register callback functions with FMOD that are called at certain points in the...
5
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem...
0
by: Francois De Serres | last post by:
Hiho, When there's an unhandled exception in my extension-module's-callback-into-Python-function-object, I get a GPF and Python exits. When the exception is being handled within the callback...
0
by: Robert | last post by:
After failing on a yield/iterator-continuation problem in Python (see below) I tried the Ruby (1.8.2) language first time on that construct: The example tries to convert a block callback interface...
7
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
0
by: Bryan | last post by:
hi, i have a multithreaded c server that calls process_method in a different c thread per each call. process_method calls a python function bar in module foo. function bar calls back into c. ...
0
by: Larry Bates | last post by:
I have a Python COM object working. I want to pass to it a Delphi callback function (showing progress) that I can call periodically. I've Googled until I'm cross-eyed and I'm getting nowhere. ...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.