[a,b,c,d] = 1,2,3,4

ast nomail at invalid.com
Tue Aug 25 17:13:44 CEST 2015


"ast" <nomail at invalid.com> a écrit dans le message de news:55dc853c$0$3083$426a74cc at news.free.fr...
>
> "Joel Goldstick" <joel.goldstick at gmail.com> a écrit dans le message de 
> news:mailman.23.1440513059.11709.python-list at python.org...
>> On Tue, Aug 25, 2015 at 10:16 AM, ast <nomail at invalid.com> wrote:
>>>>>> [a,b,c,d] = 1,2,3,4
>>>>>> a
>>>
>>> 1
>>>>>>
>>>>>> b
>>>
>>> 2
>>>>>>
>>>>>> c
>>>
>>> 3
>>>>>>
>>>>>> d
>>>
>>> 4
>>>
>>> I have never seen this syntax before. Is it documented.
>>> Is there a name for that ?
>>>
>>> thx
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>
>> its called list unpacking or packing (?)
>>
>> the right side is considered a tuple because of the commas
>>>>> a = 1,2,3
>>>>> a
>> (1, 2, 3)
>>>>> a[1]
>> 2
>>>>>
>>
>>
>> http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm
>>
>>
>> -- 
>> Joel Goldstick
>> http://joelgoldstick.com
>
> Yes you are right, it is related to tuple unpacking
> Here are some useful examples I found.
> http://svn.python.org/projects/python/branches/pep-0384/Lib/test/test_unpack_ex.py

Unpack in list

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



More information about the Python-list mailing list