* in Python

P

placid

Hi all,

Can someone tell me what * in the following code means/does a Google
search didnt turn up anything as i dont know what the * is called
(related to Python and i dont think Python has pointers)

def test(*args):
pass

and sometimes its;

def test(**args):
pass


Cheers
 
D

Duncan Booth

placid said:
Hi all,

Can someone tell me what * in the following code means/does a Google
search didnt turn up anything as i dont know what the * is called
(related to Python and i dont think Python has pointers)
* is for variable number of positional arguments, ** is for variable
keyword arguments. The syntax is symmetrical when defining functions and
when calling them.

See http://docs.python.org/ref/calls.html
and http://docs.python.org/ref/function.html
 
B

Bruno Desthuilliers

placid said:
so * basically means that args is a list

A tuple IIRC
containing more arguments that
can change in size, whereas ** means that args is a dictionary of
key=value arguments?

Why don't you try by yourself in the Python shell ? One of the nice
things with Python is that it's quite easy to explore and experiment.
 
D

Duncan Booth

Bruno said:
A tuple IIRC

In a function definition * means that any remaining position arguments will
be passed in as a tuple. In a function call the * means that any sequence
will be unpacked as positional arguments: it doesn't have to be a list or
a tuple.
 
P

placid

Bruno said:
A tuple IIRC


Why don't you try by yourself in the Python shell ? One of the nice
things with Python is that it's quite easy to explore and experiment.

i did try it in a Python shell after i learnt what it was. Like i said
*args will be a list, but when i try **args with the following code it
doesnt work

def test(**args):
keys = args.keys()
for key in keys:
print key+"="+args(key)
 
D

Duncan Booth

placid said:
i did try it in a Python shell after i learnt what it was. Like i said
*args will be a list, but when i try **args with the following code it
doesnt work

def test(**args):
keys = args.keys()
for key in keys:
print key+"="+args(key)

When you post here, it helps if instead of saying 'it doesnt work' you say
what you expected to happen and what actually happened (and quote exact
error messages, dont paraphrase them).

If I try your code it works fine: it defines a function.

If I try to call your function, even though you didn't include a call in
what 'doesnt work', then I get the exception I would expect, namely that
you are trying to call args as though it were a function 'args(key)'
instead of subscripting it as a dictionary 'args[key]'. Fixing that will
then generate a different error, but I'm sure you'll be able to figure it
out.
 
B

Bruno Desthuilliers

Duncan said:
Bruno Desthuilliers wrote:




In a function definition * means that any remaining position arguments will
be passed in as a tuple. In a function call the * means that any sequence
will be unpacked as positional arguments: it doesn't have to be a list or
a tuple.

yes, of course - I only saw it from the function's POV.
 
B

Bruno Desthuilliers

placid said:
Bruno Desthuilliers wrote:
(snip)


i did try it in a Python shell after i learnt what it was. Like i said
*args will be a list, but when i try **args with the following code it
doesnt work

"doesn't work" is the most useless description of a problem.
def test(**args):
keys = args.keys()
for key in keys:
print key+"="+args(key)

you want args[key], not args(key)

And you forget to tell how you called this code and what you got.

FWIW:
Python 2.4.3 (#1, Jun 3 2006, 17:26:11)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... for item in kw.items():
.... print "%s : %s" % item
....nose : big
parrot : dead
walk : silly
 
P

placid

Bruno said:
placid said:
Bruno Desthuilliers wrote:
(snip)


i did try it in a Python shell after i learnt what it was. Like i said
*args will be a list, but when i try **args with the following code it
doesnt work

"doesn't work" is the most useless description of a problem.
def test(**args):
keys = args.keys()
for key in keys:
print key+"="+args(key)

you want args[key], not args(key)

And you forget to tell how you called this code and what you got.

Too much world cup means sleepless nights down here in Australia or i
would have seen the error with args(key) which suppose to be args[key]
!
FWIW:
Python 2.4.3 (#1, Jun 3 2006, 17:26:11)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.... for item in kw.items():
... print "%s : %s" % item
...nose : big
parrot : dead
walk : silly

Thanks for that now i know what * means.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top