A question??

I

Ishwar Rattan

I thought that Python has no concept of reference/pointer
(probably incorrect assumption). I saw the following piece of code:

....
def run(program, *args):
pid = os.fork()
if not pid:
os.execvp(program, (program,)+args)
....

and call to run as

run("pyhton", "a.py")

What is the interpretation of *args (in def run(..)?
Looks like a reference to me, so, how does one decide when to use
a reference or not?

Any pointers to info will be appreciated.
-ishwar
 
A

Alex Martelli

Ishwar Rattan said:
I thought that Python has no concept of reference/pointer
(probably incorrect assumption).

No pointers, but any name you use, any slot in a list, any key or value
slot in a dict, etc, IS a reference. Nothing to do with your question
anyway.
def run(program, *args):
pid = os.fork()
if not pid:
os.execvp(program, (program,)+args)

and call to run as

run("pyhton", "a.py")

What is the interpretation of *args (in def run(..)?

It is: 'run' takes any number of positional arguments after the ones
specified so far. Python collects them all into a tuple and your code
refers to that tuple with the name 'args'.
Looks like a reference to me, so, how does one decide when to use
a reference or not?

'args' is a reference just like any other name is.
Any pointers to info will be appreciated.

"Python in a Nutshell", among other reference works, covers argument
passing and such subjects quite well, I think;-).


Alex
 
A

Alan Gauld

def run(program, *args):
pid = os.fork()
if not pid:
os.execvp(program, (program,)+args)
What is the interpretation of *args (in def run(..)?
Looks like a reference to me, so, how does one decide when to use
a reference or not?

Its not a reference its a placeholder for an unspecified number
of arguments. (Actually I think it's technically a tuple of
arguments?) You can think of it like the C va_arg construct
used for functions like printf().

There is also a **args construct which is for a dictionary of
keyword arguments. I'm no expert on these but thats what they are
about.

Here is the url to the documentation:

http://docs.python.org/ref/function.html

HTH,

Alan G.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top