Why can't I use the highlighted expression?

My code

$ python Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a = (1, 2) >>> '%d %d %d' % (0, *a) '0 1 2' >>> '%d %d %d' % (*a, 3) '1 2 3' >>> '%d %d' % (*a) File "<stdin>", line 1 SyntaxError: can't use starred expression here >>> 

My question is why?

In a more serious tone: I would like to receive an answer or a link in which all the options for using the selected expression are described in detail, since it happens that sometimes its behavior surprises me ...

Adding

To reflect some of the educational comments that I add the following code right after my question

 >>> '%d %d' % (, *a) File "<stdin>", line 1 '%d %d' % (, *a) ^ SyntaxError: invalid syntax >>> '%d %d' % (*a,) '1 2' >>> 

(I tried part (, a) before posting the original question, but I omitted it because the error was not related to the main role.)

In python ≥ 3.5 there is a syntax that "just works", but nonetheless I would like to get some understanding.

+8
python
source share
2 answers

This is because it is:

 (a) 

This is just a value surrounded by parentheses. This is not a new tuple object. So your expression:

 >>> '%d %d' % (*a) 

will be translated into:

 >>> '%d %d' % *a 

which is clearly incorrect in terms of python syntax.

To create a new Tuple, with one expression as an initializer, you need to add a ' , ' after it:

 >>> '%d %d' % (*a,) 

Also, if I can offer something: you can start using formatting expressions of a new style. They are gorgeous!

 >>> "{} {}".format(*a) 

You can learn more about them in the t w o paragraphs of the python documentation, this excellent site is also there. The line above uses the argument unpacking mechanism described below.

Flagged Expressions

There are many other uses for a highlighted expression than just creating a new list / tuple / dictionary. Most of them are described in this PEP and this

They all come down to two types:

Unpacking RValue:

 >>> a, *b, c = range(5) # a = 0 # b = [1, 2, 3] # c = 4 >>> 10, *range(2) (10, 0, 1) 

Iterary / dictionary initialization of an object (note that you can also unzip dictionaries inside lists!):

 >>> [1, 2, *[3, 4], *[5], *(6, 7)] [1, 2, 3, 4, 5, 6, 7] >>> (1, *[2, 3], *{"a": 1}) (1, 2, 3, 'a') >>> {"a": 1, **{"b": 2, "c": 3}, **{"c": "new 3", "d": 4}} {'a': 1, 'b': 2, 'c': 'new 3', 'd': 4} 

Of course, the most common use is unpacking the arguments:

 positional_arguments = [12, "a string", (1, 2, 3), other_object] keyword_arguments = {"hostname": "localhost", "port": 8080} send(*positional_arguments, **keyword_arguments) 

which translates as follows:

 send(12, "a string", (1, 2, 3), other_object, hostname="localhost", port=8080) 

This section has already been covered to a large extent in another overflow of the question stack.

+8
source share

My question is why?

Because your python syntax does not allow this. He defined this path, so there is no real "why."

too, it is not necessary.

 "%d %d" % a 

will work.

So, you will need to convert your extension to a tuple, and the right way to do this would be, as Lafexlos pointed out, to be

 "%d %d" % (*a,) 
+3
source share

All Articles