*args question

G

grocery_stocker

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)
 
G

grocery_stocker

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@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..... print "The formal args are: ", args
.... print "the first value is:", first
.... print "the second value is:", second
....The formal args are: ()
the first value is: 1
the second value is: 2
 
J

John Machin

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
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@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)
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
 
T

Tim Chase

grocery_stocker said:
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@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.... print "The formal args are: ", args
... print "the first value is:", first
... print "the second value is:", second
...The formal args are: ()
the first value is: 1
the second value is: 2

and if you call "myfunction(1,2,("Thread No:1",2))", you should
get something like

The formal args are: (('Thread No:1', 2),)
the first value is: 1
the second value is: 2

It's a list of the various items you put in:

def show_args(first, second, *args):
print "first", first
print "second", second
for i, arg in enumerate(args):
print "#%i %s" % (i, arg)

So you can either access "args[0]" (which is a bit dangerous, as
you assume there may be a value when there's not), or you can do
the more traditional thing of just treating it like a list as
above (e.g. iterating over it or using it in a list-comprehension).

-tkc
 
G

grocery_stocker

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@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

and if you call "myfunction(1,2,("Thread No:1",2))", you should
get something like

The formal args are: (('Thread No:1', 2),)
the first value is: 1
the second value is: 2

It's a list of the various items you put in:

def show_args(first, second, *args):
print "first", first
print "second", second
for i, arg in enumerate(args):
print "#%i %s" % (i, arg)

So you can either access "args[0]" (which is a bit dangerous, as
you assume there may be a value when there's not), or you can do
the more traditional thing of just treating it like a list as
above (e.g. iterating over it or using it in a list-comprehension).

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().
 
T

Tim Chase

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
 
T

Tim Chase

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

This should have been

thread.start_new_thread(myfunc, ("some string", 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)

and this should have been

def start_new_thread(fn, args=[], kwargs={}):
thread_magic()
result = fn(*args, **kwargs)

My threading-library usage is a little rusty, so I forgot those
were literal parameters, not args/kwargs that became more
idiomatic in other code authored later.

-tkc
 
D

Dave Angel

Try the following, to call your function yourself in this way:


def myfunction(string,sleeptime,*args):
while 1:

print "string is ", string
time.sleep(sleeptime) #sleep for a specified amount of time.

f = myfunction
r = ("Thread No:1",2)
f(*r)

The key here is the *r syntax, which is used in a function call to
turn a tuple (or list) into a separate set of arguments. It's kind of
the inverse of the *args you're already using.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top