[Tutor] Learning about callbaks

Tiger12506 keridee at jayco.net
Sat Dec 29 17:48:10 CET 2007


Callbacks are where you send python (or a library) a function which it can 
call(back). They are usually used to make things a little more generic. 
Here's a (trying to make it simple) example.

######## example.py ###########

# These first three are callback functions (nothing special
# is needed to make them a callback function)
def printtoscreen(s):
    print s

def printdifferently(s):
    s = s.upper()
    print s

def printtofile(s):
    fobj = file("output.txt","a")
    fobj.write(s)
    fobj.close()

# this is the generic function that uses callbacks
def doPrint(s, callbackfunc):
    callbackfunc(s)

# and then we can use it however we wish
# because doPrint never has to change
# no matter how, or to what we are printing.
doPrint("HelloWorld!", printtofile)
doPrint("Hello World!", printtoscreen)
doPrint("ascii", printdifferently)
##############################


----- Original Message ----- 
From: "Michael Bernhard Arp Sørensen" <michaelarpsorensen at stevnstrup.dk>
To: <tutor at python.org>
Sent: Saturday, December 29, 2007 10:58 AM
Subject: [Tutor] Learning about callbaks


Hi there.

I want to learn about callbacks because we use it at work in our software.

I there a short "hello world"-like version of a callback example?

-- 
Med venlig hilsen/Kind regards

Michael B. Arp Sørensen
Programmør / BOFH
I am /root and if you see me laughing you better have a backup.



--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list