x, = y (???)
Andrew Freeman
alif016 at gmail.com
Fri Jul 18 11:18:13 EDT 2008
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?
> 2. How could I have found this out on my own?
>
> (Regarding (2) above, I consulted the index of several Python
> reference books but I could not find the answer to (1). I hope to
> find a better Python reference!)
>
> TIA!
>
> kynn
>
Try this:
>>> y = 'abc'
>>> type(y)
<type 'str'>
>>> type((y,))
<type 'tuple'>
>>> y = y, # y, is just short for (y,) you *have* to use a coma in 1
length tuples
>>> y
('abc',)
>>> type(y)
<type 'tuple'>
>>> y[0]
'abc'
>>> x, = y #same as x = y[0] OR more verbosely (x,) = (y,)
>>> x
'abc'
>>> type(x)
<type 'str'>
Maybe that will hape you understand what the comma is for?
More information about the Python-list
mailing list