Re: x, = y (???)

2008-07-18 Thread Andrew Freeman

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?
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-18 Thread kj
In [EMAIL PROTECTED] Matthew Woodcraft [EMAIL PROTECTED] writes:

kj wrote:
 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.


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[s]) == 1:
## If there is only one value (d2) left in square, remove it from peers
d2, = values[s]

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

d2 = values[s]

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

Thanks!

kynn
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Erik Max Francis

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


It's unpacking a 1-tuple:

(x,) = y

The parentheses here are not necessary and are sometimes left out.

--
Erik Max Francis  [EMAIL PROTECTED]  http://www.alcyone.com/max/
 San Jose, CA, USA  37 18 N 121 57 W  AIM, Y!M erikmaxfrancis
  The United States, while they wish for war with no nation, will buy
   peace with none. -- James Madison
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Chris Mellon
On Thu, Jul 17, 2008 at 2:55 PM, kj [EMAIL PROTECTED] wrote:
 In [EMAIL PROTECTED] Erik Max Francis [EMAIL PROTECTED] 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

--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Jonathan Gardner
On Jul 17, 12:55 pm, kj [EMAIL PROTECTED] wrote:
 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',)
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Jonathan Gardner
On Jul 17, 12:55 pm, kj [EMAIL PROTECTED] wrote:
 what's the point of performing such unpacking?

record = [name, address, telephone]

...

name, address, telephone = record
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Matthew Woodcraft
kj wrote:
 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-
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Duncan Booth
kj [EMAIL PROTECTED] wrote:

 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?

None whatsoever when the string has only one character, but with 2 
characters it becomes an obfuscated way of checking that there was only one 
character:

 x, = 'a'
 x, = 'ab'

Traceback (most recent call last):
  File pyshell#29, line 1, in module
x, = 'ab'
ValueError: too many values to unpack
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread Terry Reedy



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?


1.Experiment with the interactive interpreter.  It is sooo easy.

 x, = '1'
 x
'1'
 x, = '12'
Traceback (most recent call last):
  File pyshell#53, line 1, in module
x, = '12'
ValueError: too many values to unpack
 x,y = '12'
 x,y
('1', '2')
 x, = 3
Traceback (most recent call last):
  File pyshell#45, line 1, in module
x, = 3
TypeError: 'int' object is not iterable
 *x, = [3]
 x
[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.

--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread DaveM
On Thu, 17 Jul 2008 17:32:30 -0400, Terry Reedy [EMAIL PROTECTED] wrote:


  *x, = [3]
  x
[3]

What does *x signify?

DaveM
--
http://mail.python.org/mailman/listinfo/python-list


Re: x, = y (???)

2008-07-17 Thread alex23
On Jul 18, 8:01 am, DaveM [EMAIL PROTECTED] wrote:
 On Thu, 17 Jul 2008 17:32:30 -0400, Terry Reedy [EMAIL PROTECTED] wrote:
   *x, = [3]
   x
 [3]

 What does *x signify?

Mostly that Terry is using Python 3.0.

See: http://www.python.org/dev/peps/pep-3132/

--
http://mail.python.org/mailman/listinfo/python-list


Re: x, y coordinates in Tkinter canvas

2005-11-11 Thread jepler
Let's find out!  This program creates a series of rectangles, with the color
going from black to nearly white.  White corresponds to higher x and y
coordinate values.  So the answer to your question can be seen in the output
of the program.

import Tkinter

c = Tkinter.Canvas(width=220, height=220)
c.pack()

for i in range(0, 200, 20):
lum = i * 255 / 200
color = #%02x%02x%02x % (lum, lum, lum)
c.create_rectangle( i, i, i+40, i+40, fill=color)

c.mainloop()


pgpSbn6OJyaNx.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list