*args question
John Machin
sjmachin at lexicon.net
Wed Mar 25 11:19:05 EDT 2009
On Mar 26, 1:17 am, grocery_stocker <cdal... at gmail.com> wrote:
> On Mar 25, 7:05 am, grocery_stocker <cdal... at gmail.com> wrote:
>
>
>
> > Given the following code...
>
> > #!/usr/bin/env python
>
> > import time
> > import thread
>
> > def myfunction(string,sleeptime,*args):
> > while 1:
>
> > print string
> > time.sleep(sleeptime) #sleep for a specified amount of time.
>
> > if __name__=="__main__":
>
> > thread.start_new_thread(myfunction,("Thread No:1",2))
>
> > while 1:pass
>
> > Taken from following URL....http://linuxgazette.net/107/pai.html
>
> > How can myfunction() extract the tuple ("Thread No:1",2) from
> > start_new_thread() if myfunction is only being passed the single arg
> > ("Thread No:1",2)
>
> The only thing that I think of is that the tuple ("Thread No:1",2) is
> somehow being extract before it gets passed to myfunction(). Ie,
> something like the following...
>
> [cdalten at localhost ~]$ python
> Python 2.4.3 (#1, Oct 1 2006, 18:00:19)
> [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> def myfunction(first, second, *args):
>
> ... print "The formal args are: ", args
> ... print "the first value is:", first
> ... print "the second value is:", second
> ...>>> a, b = (1,2)
> >>> myfunction(a,b)
>
> The formal args are: ()
> the first value is: 1
> the second value is: 2
Manual sez:
thread.start_new_thread(function, args[, kwargs])
where args must be a tuple
so thread.start_new_thread does this (ignoring the kwargs):
function(*args)
or it would if it were written in Python instead of C.
HTH
John
More information about the Python-list
mailing list