Asterisk sign in python

S

sarmin

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
 
J

John Roth

sarmin said:
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
 
D

David M. Cooke

John Roth said:
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

:)
 
M

Myles

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(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

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
required 10
necessary 20
optional (30, 40)
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top