x, = y (???)

K

kj

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
 
J

Jonathan Gardner

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"?

Try:

y = "abc"
x, = y

You were unpacking y into ('Y',)
 
C

Chris Mellon

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!
 
M

Matthew Woodcraft

kj said:
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"?

If y really is is a string, I think it's likely that the line you came
across was a typo.

In the case you give above, there's no difference at the end between x
and y.

If y had length other than 1, the second line would raise an exception,
so it's not the same as plain "x = y". But if that's the intended effect,
it's a daft way to write it.

-M-
 
T

Terry Reedy

kj said:
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?

1.Experiment with the interactive interpreter. It is sooo easy.
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
x, = '12'
ValueError: too many values to unpackTraceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
x, = 3
TypeError: 'int' object is not iterable
[3]

2. Read the Python docs which come with the interpreter.
LanguageReference/SimpleStatements/AssignmentStatements:
"If the target list is a comma-separated list of targets:
....
Else: The object must be a sequence with the same number of items as
there are targets in the target list, and the items are assigned, from
left to right, to the corresponding targets."
which is exactly the behavior observed above.
 
A

Andrew Freeman

kj said:
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((y,))
>>> y = y, # y, is just short for (y,) you *have* to use a coma in 1 length tuples
>>> y ('abc',)
>>> type(y)
>>> 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?
 
K

kj

If y really is is a string, I think it's likely that the line you came
across was a typo.


OK, this is the best explanation I've seen for the code I'm talking about.

This code may be found at:

http://norvig.com/sudo.py

in the definition of the function eliminate. Here's the fragment:

elif len(values) == 1:
## If there is only one value (d2) left in square, remove it from peers
d2, = values

Now, in the assignment, values *is* a string of length 1. So
this assignment appears to me entirely equivalent (in its ultimate
effect) to the more straightforward

d2 = values

....but, since I'm a noob, I thought I'd ask :)

Thanks!

kynn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top