8
Suppose there is a tuple with three values, ('a', 1, 'b')
, and you want to assign the first value in one variable and keep the rest in another. For this you can do:
a, *b = ('a', 1, 'b')
Thus, a
will possess the value 'a'
while b
will possess the value [1, 'b']
.
Why the value defined in b
is a list?
I cannot see the advantages of creating a changeable structure, like the list, from a fixed value, as I also see no reason to use a structure that semantically should be homogeneous to allocate possibly heterogeneous data.
So much so that when used as argument of a function the created object is a tuple:
def foo(a, *b):
print(a, b)
foo('a', 1, 'b') # a (1, 'b')
It wouldn’t make more sense b
also be a tuple in a, *b = ('a', 1, 'b')
?
But the question is precisely why "unpack" creates a list. I also didn’t understand the relationship of
namedtuple
with the question.– Woss
Because the tuple itself is immutable. As I said " if you do unpacking, it is because you will manipulate the data from the variables you used for unpacking.". The namedtuple is so there is no need to unpack and be able to use the tuple as if it were a variable, there is no need to unpacking. I’ll try to be more concise in my answer.
– SakuraFreak
1) "by default *args assembles a list": the AP proves that when the unpacking is made in the arguments of a function, the return is a tuple and not a list. Your statement makes no sense. 2) "Unpack serves to instantiate an immutable value in a mutable variable": I read the PEP 3132 and I didn’t find that statement. 3) "If you want to work with named tuples, the recommended is to use namedtuple": I didn’t understand the connection with the question and
namedtuples
is the only native implementation of Named Tuples, so it seems obvious to me the statement– fernandosavio
"This PEP proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be Assigned a list of all items not Assigned to a "regular" name." namedtuple so there is no need to save in memory a tuple that may already have been created. And there is no need to "find" this statement, it is simple. Tuples are immutable, and have only two senses of unpacking, manipulating data, or naming them. So I quoted namedtuples. The knowledge I’m based on is in a book written by Luciano Ramalho, Fluent Python.
– SakuraFreak
I have Fluent Python, I’ll take a look when I get home. And even with what you mentioned, your statement "Unpack serves to instantiate an immutable value in a mutable variable" still doesn’t make sense. And maybe it wasn’t clear, but the AP would like to understand why they decide for lists instead of tuples and why this behavior isn’t the same when we unpack positional arguments. And just a discussion to understand why this decision was made, we know it’s a list, but it should be a list?
– fernandosavio
Hmm, so, but there it is. What would be the meaning in unpack a tuple for another tuple? That’s what I’m putting on the agenda. If only to name the values of the tuple, wouldn’t it be more interesting to use namedtuple for this case? What would be the sense of creating a tuple from another tuple(?)
– SakuraFreak
a, *b, c = (1, 2, 3, 4, 5)
would be1, (2, 3, 4), 5
.. It would make as much sense as a list because you are breaking a sequence into pieces, with the difference that semantically a tuple would make more sense because it is a fixed representation of the "piece" of the sequence. The discussion is legal, the difficult thing is to find an answer as to why this decision was made this way, because in PEP 3132 it is specified that it is a list, but it does not explain why it has to be a list. PS: Which Fluent Python chapter did you refer to? I won’t be able to read it all now until this afternoon hahahah– fernandosavio
Anything goes to Sopt’s chat in the afternoon... here is too long already
– fernandosavio
page 27 begins Tuple Unpacking. I have to go to work, but I will go there to chat
– SakuraFreak
(did not see how was the previous text of the reply - but the current answer seems satisfactory to me)
– jsbueno