My code:
import threading
def hello(arg, kargs):
print arg
t = threading.Timer(2, hello, "bb")
t.start()
while 1:
pass
The print out put is just:
b
How can I pass a argument to the callback? What does the kargs mean?
Timer takes an array of arguments and a dict of keyword arguments, so you need to pass an array:
import threading
def hello(arg):
print arg
t = threading.Timer(2, hello, ["bb"])
t.start()
while 1:
pass
You're seeing "b" because you're not giving it an array, so it treats "bb" an an iterable; it's essentially as if you gave it ["b", "b"].
kwargs is for keyword arguments, eg:
t = threading.Timer(2, hello, ["bb"], {arg: 1})
See http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html for information about keyword arguments.
The third argument to Timer is a sequence. Since you pass "bb" as that sequence, hello gets the elements of that sequence ("b" and "b") as separate arguments (arg and kargs). Put "bb" in a list and hello will get the string as the first argument.
t = threading.Timer(2, hello, ["bb"])
As for hello's parameters, you probably mean:
def hello(*args, **kwargs):
The meaning of **kwargs is covered in the queston "What does *args and **kwargs mean?"