x, = y (???)

Chris Mellon arkanes at gmail.com
Thu Jul 17 22:03:52 CEST 2008


On Thu, Jul 17, 2008 at 2:55 PM, kj <socyl at 987jk.com.invalid> wrote:
> In <crydndTBLuLcBuLVnZ2dnUVZ_jWdnZ2d at speakeasy.net> Erik Max Francis <max at alcyone.com> writes:
>
>>kj wrote:
>
>>> I just came across an assignment of the form
>>>
>>>   x, = y
>>>
>>> where y is a string (in case it matters).
>>>
>>> 1. What's the meaning of the comma in the LHS of the assignment?
>
>>It's unpacking a 1-tuple:
>
>>       (x,) = y
>
>>The parentheses here are not necessary and are sometimes left out.
>
> I still don't get it.  If we write
>
>  y  = 'Y'
>  x, = y
>
> what's the difference now between x and y?  And if there's no
> difference, what's the point of performing such "unpacking"?
>
> TIA!

>>> y = (1,)
>>> y
(1,)
>>> x, = y
>>> x
1
>>>



More information about the Python-list mailing list