*args question

Tim Chase python.list at tim.thechases.com
Wed Mar 25 12:13:35 EDT 2009


> Maybe I'm missing it, but in the original code, the line had
> 
> thread.start_new_thread(myfunction,("Thread No:1",2))
> 
> It has a single arg  ("Thread No:1",2) versus something like
> 
> thread.start_new_thread(myfunction,1, 2, ("Thread No:1",2))
> 
> But
> 
> def myfunction(string,sleeptime,*args):
> 
> clearly takes two args. I don't get how the single arg ("Thread No:1",
> 2) in start_new_thread() gets magically converted two arges, string
> and sleeptime, before it reaches myfunction().

As John pointed out, the start_new_thread passes *args to your 
function.  So you would define a your function:

   def myfunc(some_string, some_int):
     print "The string value:", some_string
     print "The int value", some_int

   thread.start_new_thread(myfunc, "some string", 42)

should print

   The string value: some string
   The int value: 42

because all the subsequent values after the function-handle/name 
get passed into the function when it gets called.  As if the 
start_new_thread() function was defined as

   def start_new_thread(fn, *args, **kwargs):
     thread_magic_happens_here()
     result = fn(*args, **kwargs)
     return more_thread_magic_happens_here(result)

-tkc







More information about the Python-list mailing list