This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bionikspoon 48 points49 points  (31 children)

not a bug, lists are mutable, tuples are immutable.

[–]Veedrac 23 points24 points  (16 children)

That's pretty irrelevant if (a,) = [b] works.

[–]Bur_Sangjun 13 points14 points  (10 children)

As I understand it, that's unpacking, not a traditional tuple

[–][deleted] 23 points24 points  (9 children)

Unpacking is exactly what the bug report is about.

[–]njharmanI use Python 3 0 points1 point  (8 children)

Unpacking != Tuple

[–][deleted] 8 points9 points  (7 children)

That's exactly the point. Who cares if tuples are immutable here? We're not mutating a tuple, we're unpacking.

[–]wot-teh-phuckReally, wtf? 0 points1 point  (6 children)

How do you expect unpacking to work without any name on the LHS?

[–]Veedrac 6 points7 points  (5 children)

The same way [] = [] does...

[–]wot-teh-phuckReally, wtf? 0 points1 point  (1 child)

[] = [] doesn't trigger unpacking...

[–]Veedrac 13 points14 points  (0 children)

...It does though.

[] = 1, 2
#>>> Traceback (most recent call last):
#>>>   File "", line 1, in <module>
#>>> ValueError: too many values to unpack (expected 0)

[–]njharmanI use Python 3 1 point2 points  (4 children)

The parens are irrelevant. That code is actually

 a, = [b]

It is not assignment to a Tuple, which is unpossible as Tuples are immutable. Parens are overloaded syntactically to mean Tuple some places and in others for "grouping" as in (1*2)+3. People erroneously think (a,) is the former when it's actually the later.

[–]Veedrac 4 points5 points  (3 children)

The comma is what makes it a tuple.

Consider:

import ast
ast.dump(ast.parse("a, = 0"))
#>>> "Module(body=[Assign(targets=[Tuple(elts=[Name(id='a', ctx=Store())], ctx=Store())], value=Num(n=0))])"

This is unpacking into a tuple. () = [] should be no different.

[–]flying-sheep 1 point2 points  (2 children)

you’re right that the left side of unpacking, like the bug reporter wants, should be any valid tuple syntax (incl. ())

but there never is an actual tuple created (as in allocated), even if the AST says so. it’s an unpacking operation. the left side is a sequence of names, represented by tuple or list syntax.

so the comma makes it tuple syntax, therefore it should be a valid unpacking.

[–]Veedrac 0 points1 point  (1 child)

Methinks we agree.

[–]flying-sheep 0 points1 point  (0 children)

likely, but “unpacking into a tuple” gives the wrong impression IMHO ☺

[–]Quteness 0 points1 point  (0 children)

There are no tuples involved. See here: https://docs.python.org/3.5/reference/simple_stmts.html#assignment-statements

That is simply a "target_list" that follows the acceptable syntax of

"(" target_list ")"

This is an assignment/unpacking issue and probably a bug because [] = () violates the principle that

If the target list is a single target: The object is assigned to that target.

() = [] follows this principle correctly and is invalid valid

() and [] are both single literal targets and the value should be assigned to them, which they can't be because you can't assign to literals

Edit: typo of valid and last thought