technovelty

weblog of Ian Wienand

RSS  |  technovelty home  |  page of ian  |  ian@wienand.org

on asterisks in Python

Asterisks have lots of meaning in Python. Firstly, consider in a function definition

>>> def function(arg, *vargs, **kargs):
	print arg
	print vargs
	print kargs
>>> function(1, 2,3,4,5, test1="abc", test2="def")
1
(2, 3, 4, 5)
{'test1': 'abc', 'test2': 'def'}

On the other hand, you can use the asterisk with a tuple when calling a function to expand out the elements of the tuple into positional arguments

>>> def function(arg1, arg2, arg3):
    print arg1, arg2, arg3
>>> args = (1,2,3)
>>> function(*args)
1 2 3

You can do a similar thing with keyword arguments and a dictionary with the double asterisk operator

>>> def function(arg1=None, arg2=None):
     print arg1, arg2
>>> dict = {"arg1":"1", "arg2":"2"}
>>> function(**dict)
1 2

posted at: Tue, 05 Apr 2005 14:04 | in /code/python | permalink | add comment (9 others)

Posted by arbitrarymeaningless at Sun Sep 19 22:50:29 2010

Congratulations.  This page is number one in the Google search results for python asterisk operator.

Extremely useful. Thank you.  I kept thinking it was the dereferencing operator from C.

Posted by instanceofTom at Fri Jan 21 08:08:38 2011

Also the first google result for: "python double asterisk".

Thanks for the useful post.

Posted by Donn Lee at Tue Apr 19 05:34:31 2011

While reading code, I came across use of ** (double asterisk), the 3rd example in this post.  Thanks for spelling it out!

Posted by Feelmjawlk at Tue Jun 14 03:50:26 2011

Superb! Clarity as its best.

Posted by Casey Strouse at Mon Jun 20 17:41:55 2011

I had  no idea what the asterisk operator did.  I learn something new about Python everyday.  Thanks.

Posted by Anoop at Thu Sep 1 22:05:07 2011

Thanks a lot

Posted by TimBo at Thu Nov 24 17:16:21 2011

You can also use an asterisk with print to print the  string on the left however many times you specify on the right

print "." * 10

will produce:

..........

although i am not able to find this usage in the documentation anywhere, so if anyone knows what this is called with a description, i would really appreciate it

Posted by curlyice at Sun Nov 27 08:22:04 2011

The asterisk is overloaded in Python to make concatenated shallow copies of a sequence type, provided the other operand is an int. Because str is a sequence type, that asterisk produces a str containing 10 shallow copies of the str literal (it's equivalent to a deep copy in this case).. Here's an example showing that the copying is indeed shallow:

>>> a = [1,2,3]
>>> b = [a] * 5
>>> b
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> a[2] = 4
>>> b
[[1, 2, 4], [1, 2, 4], [1, 2, 4], [1, 2, 4], [1, 2, 4]]

Posted by Damian at Tue Dec 13 22:57:07 2011

Double asterisk is also used as an indicator for exponents (to the power of) as in:

2**2=4

Add a comment
*Name
*Email (not shown)
Website
*Comment:
Anti-spam:
* denotes required field

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.