473,549 Members | 3,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

* in Python

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

Jun 23 '06 #1
10 1078
placid wrote:
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


see sections 4.7.2 and 4.7.3 in the tutorial:

http://docs.python.org/tut/node6.htm...00000000000000

for more details, see the description of the "def" statement in the
language reference.

</F>

Jun 23 '06 #2
placid wrote:
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
Jun 23 '06 #3

Duncan Booth wrote:
placid wrote:
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


so * basically means that args is a list containing more arguments that
can change in size, whereas ** means that args is a dictionary of
key=value arguments?

Jun 23 '06 #4
placid wrote:
Duncan Booth wrote:
placid wrote:

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

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.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jun 23 '06 #5
Bruno Desthuilliers wrote:
so * basically means that args is a list


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.
Jun 23 '06 #6

Bruno Desthuilliers wrote:
placid wrote:
Duncan Booth wrote:
placid wrote:
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

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.


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(ke y)


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"


Jun 23 '06 #7
placid wrote:
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(ke y)


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.
Jun 23 '06 #8
Duncan Booth wrote:
Bruno Desthuilliers wrote:

so * basically means that args is a list


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.


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

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jun 23 '06 #9
placid wrote:
Bruno Desthuilliers wrote:
(snip)
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


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


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.
def test(**kw): .... for item in kw.items():
.... print "%s : %s" % item
.... test()
test(parrot="de ad", walk="silly", nose="big") nose : big
parrot : dead
walk : silly test(**{'answer ':42}) answer : 42

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jun 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7718
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.