48

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, why?

In a more serious tone: I'd like an answer, or a reference, that details all the ins and outs of using a starred expression, as it happens that I am sometimes surprised from its behaviours...

Addendum

To reflect some of the enlightening comments that immediately followed my question I add the following code

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

(I had tried the (, a) part before posting the original question but I've omitted it 'cause the error was not related to the starring.)

There is a syntax, in python ≥ 3.5, that "just works" but nevertheless I would like some understanding.

Błażej Michalik
  • 3,727
  • 34
  • 47
gboffi
  • 19,456
  • 5
  • 52
  • 81
  • 2
    `(*a,)` Note the comma at the end to make it tuple. – Lafexlos Nov 18 '16 at 11:37
  • 1
    @MarcusMüller extending unpacking (for `*` and `**` on the RHS) was 3.5. And the correct syntax is `(*a,)`... – Jon Clements Nov 18 '16 at 11:38
  • @JonClements **I** know. That's why I asked gboffi above to clarify the python version she/he uses!! – Marcus Müller Nov 18 '16 at 11:39
  • Take a look at the "Unpacking Iterables" documentation on SO https://stackoverflow.com/documentation/python/809/compatibility-between-python-3-and-python-2/2845/unpacking-iterables#t=201611181159516910043 – pylang Nov 18 '16 at 12:01
  • Interestingly, the `format()` function does not have this issue, i.e. `'{:d} {:d}'.format(*a)` --> `'1, 2'`. The issue appears related to the `%` string formatter. – pylang Nov 18 '16 at 12:08
  • @pylang SO documentation is a resource that I have not internalized yet... thank you for the pointer – gboffi Nov 18 '16 at 12:10
  • @pylang That's because you are using argument unpacking there, not trying to create a tuple. See my edited answer below. – Błażej Michalik Nov 18 '16 at 12:34

3 Answers3

54

It's because this:

(a)

Is just a value surrounded by parenthesis. It's not a new tuple object. So your expression:

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

will get translated to:

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

which is obviously wrong in terms of python syntax.

In order to create a new tuple, with one expression as an initializer, you need to add a ',' after it:

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

Note: unless a is a generator, in this particular situation you could just type:

>>> '%d %d' % a

Also, if I may suggest something: you could start using new-style formating expressions. They are great!

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

You can read more about them in those two paragraphs of python documentation, also there is this great website. The line above uses argument unpacking mechanism described below.

Update: since python 3.6, you could also use string interpolation - f-strings! These are described in PEP-498, and some examples can be found in Python documentation.

Starred Expressions

There are many more uses to starred expression than just creating a new list/tuple/dictionary. Most of them are described in PEP 3132, and PEP 448.

All of them come down to two kinds:

R-value unpacking:

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

Iterable / dictionary object initialization (notice that you can unpack dictionaries inside lists too!):

>>> [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 often seen use is arguments unpacking:

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

which would translate to this:

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

This topic has already been covered to a substantial extent in another Stack Overflow question.

bad_coder
  • 8,684
  • 19
  • 37
  • 59
Błażej Michalik
  • 3,727
  • 34
  • 47
3

My question, why?

Because your python syntax doesn't allow that. It's defined that way, so there's no real "why".

also, it's unnecessary.

"%d %d" % a

would work.

So, you'd need to convert your expansion to a tuple – and the right way of doing that would be, as pointed out by Lafexlos, be

"%d %d" % (*a,)
Marcus Müller
  • 31,250
  • 4
  • 47
  • 86
  • 1
    Re `"%d %d" % a`, my _real_ use case involves a generator expression…. In the question I wanted to keep things simple and, moreover, I really want to know the details of unpacking – gboffi Nov 18 '16 at 12:06
0

It's because:

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

Can be just:

>>> '%d %d' %a

Of course then able to do:

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

But then:

>>> (*a,)==a
True
>>> 

Or you can do:

>>> '%d %d' % [*a]

But then:

>>> [*a]
[1, 2]
>>> a
(1, 2)
>>> 

So:

>>> tuple([*a])==a
True
U12-Forward
  • 65,118
  • 12
  • 70
  • 89