By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,736 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

Asterisk sign in python

P: n/a
Hi Pythoners...

It seems to me the Asterisk (*) sign in python means many things...

if a python code line looks like this:
def__init__(self, func, *param):
bla bla bla...

and then u have something like this:
func(*param)

and then something like this:
def Wrapper(select, func, param):
bla bla bla...

I took this section of code from the 'Python Cookbook' textbook page
223 about 'Running Functions in the future'...

I just cant figure out what is the asterisk (*) sign for and what is
the different between *param and param??

Many thanks Pythoners...

Regards
sarmin
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"sarmin" <sa********@yahoo.com> wrote in message
news:ma*************************************@pytho n.org...
Hi Pythoners...

It seems to me the Asterisk (*) sign in python means many things...

if a python code line looks like this:
def__init__(self, func, *param):
bla bla bla...

and then u have something like this:
func(*param)

and then something like this:
def Wrapper(select, func, param):
bla bla bla...

I took this section of code from the 'Python Cookbook' textbook page
223 about 'Running Functions in the future'...

I just cant figure out what is the asterisk (*) sign for and what is
the different between *param and param??

Many thanks Pythoners...

Regards
sarmin
It's for variable length parameter lists. In a function
definition, one * is a list of additional positional
parameters, two **s is a dictionary of additional
keyword parameters.

In a function call, one * is a list of additional
positional parameters, two **s is a dictionary
of additional keyword parameters.

John Roth

Jul 18 '05 #2

P: n/a
At some point, "John Roth" <ne********@jhrothjr.com> wrote:
"sarmin" <sa********@yahoo.com> wrote in message
news:ma*************************************@pytho n.org...
Hi Pythoners...

It seems to me the Asterisk (*) sign in python means many things...

if a python code line looks like this:
def__init__(self, func, *param):
bla bla bla...

and then u have something like this:
func(*param)

and then something like this:
def Wrapper(select, func, param):
bla bla bla...

I took this section of code from the 'Python Cookbook' textbook page
223 about 'Running Functions in the future'...

I just cant figure out what is the asterisk (*) sign for and what is
the different between *param and param??

Many thanks Pythoners...

Regards
sarmin


It's for variable length parameter lists. In a function
definition, one * is a list of additional positional
parameters, two **s is a dictionary of additional
keyword parameters.

In a function call, one * is a list of additional
positional parameters, two **s is a dictionary
of additional keyword parameters.


Don't forget multiplicaton:

2 * 2

and power

2 ** 2

:-)

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #3

P: n/a
Hi Sarmin,
and then u have something like this:
func(*param) [...] I just cant figure out what is the asterisk (*) sign for and what is
the different between *param and param??


The asterisk is used for multiple parameters, which are passed to
the function as a tuple.

If your function might be called with a varying number of arguments:

myfunc(10)
or
myfunc(10, 20, 30)
or
myfunc(10, "ten", 20, "twenty")

you could write it as:

def myfunc(*param):
print param

and you would get
myfunc(10) (10,) myfunc(10, 20, 30) (10, 20, 30) myfunc(10, "ten", 20, "twenty") (10, 'ten', 20, 'twenty')

If you had defined your function without the
asterisk, you could only use a single parameter:

def myfunc(param):
print param
myfunc(10) 10 myfunc(10, 20, 30)
Traceback (most recent call last):
File "<pyshell#27>", line 1, in -toplevel-
myfunc(10, 20, 30)
TypeError: myfunc() takes exactly 1 argument (3 given)

You can have non-optional parameters before the asterisk parameter:

def myfunc(required, necessary, *optional):
print "required", required
print "necessary", necessary
print "optional", optional
myfunc(10, 20, 30, 40) required 10
necessary 20
optional (30, 40) myfunc(10)


Traceback (most recent call last):
File "<pyshell#35>", line 1, in -toplevel-
myfunc(10)
TypeError: myfunc() takes at least 2 arguments (1 given)
Regards, Myles.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.