473,910 Members | 4,240 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 1110
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
11349
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11055
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8099
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6142
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4776
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
2
4337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3360
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.