473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Asterisk sign in python

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
3 4112
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sarmin kho | last post by:
Hi Pythoners... I am definitely confused with the use of asterisk (*) sign in python... I quoted the following code from 'python cookbook' text book page 223 about 'running functions in the...
29
by: Mark Hahn | last post by:
We are considering switching to the dollar sign ($) for self, instead of the period ( . ) we are using now in Prothon. Ruby uses the at-sign (@) for self, but our new usage of self also includes...
1
by: garett | last post by:
Hello, I have been reading text processing in python and in the appendix the author describes: >>> sides = >>> zip(*zip(*sides)) what is this asterisk-list syntax called? Any suggestions for...
3
by: Robert M | last post by:
Hello, I am having a problem when I enter a new row using data grid footer and the data contains asterisk ('). I can use replace function, and it will not crash the program but it shows 2...
2
by: Evan | last post by:
I am trying to put an asterisk in the Label text property but the asterisk doesn't show up in the correct place. If I enter "* hello" in the text property (using the designer property pages) the...
0
by: selvarani | last post by:
Hi all, I am a newbie to Asterisk and PHP. I have installed wamp5_1.4.3 and working with PHP on this. I would like to write a PHP script to interact with asterisk. Where are the Manager API's...
1
by: striker07 | last post by:
Someone help me how can i display a diamond shaped asterisk correctly pls. correct my program this is my code: Dim ctr, space, asterisk As Integer For ctr = 1 To 9 Step 2 For space = (9...
2
by: Coll | last post by:
I'm working with a form & a query that allows the user to filter the data using several combo boxes. One of the combo boxes allows them to select the first letter of a a particular field, and it...
9
by: Phillip B Oldham | last post by:
Are there any FOSS Python Single-Sign-on Servers? We're looking to centralise the sign-on for our numerous "internal" webapps (across multiple servers, languages, and domains) to speed user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.