What is the meaning of the astarisk in Python

J

John Dean

Hi
Below is a line taken from Andy Dustman's MySQLdb module:
def Connect(*args, **kwargs):

I would be grateful if somebody explain the meaning of the astarisks, since
I was under the impression that pointers are not used in Python and the
above look very must like pointers to me
 
J

John Roth

John Dean said:
Hi
Below is a line taken from Andy Dustman's MySQLdb module:
def Connect(*args, **kwargs):

I would be grateful if somebody explain the meaning of the astarisks, since
I was under the impression that pointers are not used in Python and the
above look very must like pointers to me

One asterisk creates a list of all positional arguements,
two asterisks creates a dictionary of all keyword arguements.
"All" means "not otherwise represented in the parameter list."

John Roth
 
A

Andrei

They're both shortcuts for specifying parameters. args is a tuple of
positional arguments, kwargs is a dictionary of keyword arguments.
Like this:



... print args

...

(3, 4, 'c')

... print kwargs

...

{'a': 3, 'b': 4}

... print args, kwargs

...

(2, 3) {'c': 5, 'd': 6}

(2, 3) {}

() {'c': 5, 'd': 6}


--
Contact info (decode with rot13): (e-mail address removed)
Fcnzserr! Cyrnfr qb abg hfr va choyvp zrffntrf. V ernq gur yvfg, ab arrq gb PP.


Posted via http://dbforums.com
 
B

Bob Gailer

At 06:13 PM 9/20/2003, John Roth wrote:

One asterisk creates a list of all positional arguements,

Do you mean tuple?
two asterisks creates a dictionary of all keyword arguements.
"All" means "not otherwise represented in the parameter list."

Bob Gailer
(e-mail address removed)
303 442 2625
 
M

Michael Peuser

John Dean said:
Hi
Below is a line taken from Andy Dustman's MySQLdb module:
def Connect(*args, **kwargs):

I would be grateful if somebody explain the meaning of the astarisks, since
I was under the impression that pointers are not used in Python and the
above look very must like pointers to me

In addition to the other answers:
It also works the other way: You can use tuples ore dicts for positional or
keyword parameters. This generally comes handy for co-ordinates in graphic
routines.

def p(a,b,c,d, x=None,y=None):
print a,b,c,d,x,y

abcd=(1,2,3,4)
xy={'x':100, 'y':200}

p(*abcd)
p(0,0,0,0,**xy)


Kindly
MichaelP
 
L

Lee Harr

Hi
Below is a line taken from Andy Dustman's MySQLdb module:
def Connect(*args, **kwargs):

I would be grateful if somebody explain the meaning of the astarisks, since
I was under the impression that pointers are not used in Python and the
above look very must like pointers to me


http://python.org/doc/current/tut/node6.html#SECTION006600000000000000000
http://python.org/doc/current/tut/node6.html#SECTION006720000000000000000
http://python.org/doc/current/tut/node6.html#SECTION006730000000000000000
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top