473,563 Members | 2,709 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 1081
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
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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
5213
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
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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.