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
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded. 12 1003
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 && ma*@alcyone.com && 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
In <cr******************************@speakeasy.netEri k Max Francis <ma*@alcyone.comwrites:
>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!
kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
On Jul 17, 12:55*pm, kj <so...@987jk.com.invalidwrote:
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',)
On Jul 17, 12:55*pm, kj <so...@987jk.com.invalidwrote:
what's the point of performing such "unpacking"?
record = [name, address, telephone]
...
name, address, telephone = record
On Thu, Jul 17, 2008 at 2:55 PM, kj <so***@987jk.com.invalidwrote:
In <cr******************************@speakeasy.netEri k Max Francis <ma*@alcyone.comwrites:
>>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
>>>
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-
kj <so***@987jk.com.invalidwrote:
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
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.
On Thu, 17 Jul 2008 17:32:30 -0400, Terry Reedy <tj*****@udel.eduwrote:
>*x, = [3] x
[3]
What does *x signify?
DaveM
On Jul 18, 8:01*am, DaveM <asm...@dsl.pipex.comwrote:
On Thu, 17 Jul 2008 17:32:30 -0400, Terry Reedy <tjre...@udel.eduwrote:
>>*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/
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?
In <BZ*******@news.chiark.greenend.org.ukMatthew Woodcraft <ma******@chiark.greenend.org.ukwrites:
>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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William C. White |
last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using
cURL? Our website is hosted on a shared drive and the webhost company
doesn't installed additional software (such as cURL)...
|
by: Albert Ahtenberg |
last post by:
Hello,
I don't know if it is only me but I was sure that header("Location:url")
redirects the browser instantly to URL, or at least stops the execution of
the code. But appearantely it continues...
|
by: James |
last post by:
Hi,
I have a form with 2 fields.
'A'
'B'
The user completes one of the fields and the form is submitted.
On the results page I want to run a query, but this will change
subject to which...
|
by: Ollivier Robert |
last post by:
Hello,
I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9
system. The link succeeds but everytime I try to run php, I get a SEGV from
inside the libcnltsh.so library.
...
|
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the...
|
by: Albert Ahtenberg |
last post by:
Hello,
I have two questions.
1. When the user presses the back button and returns to a form he filled
the form is reseted. How do I leave there the values he inserted?
2. When the...
|
by: inderjit S Gabrie |
last post by:
Hi all
Here is the scenerio ...is it possibly to do this...
i am getting valid course dates output on to a web which i have designed
....all is okay so far , look at the following web url
...
|
by: Jack |
last post by:
Hi All,
What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g.
select x from y where z=:parameter
Which in asp/jsp would be followed by some statements to bind a value...
|
by: Sandwick |
last post by:
I am trying to change the size of a drawing so they are all 3x3.
the script below is what i was trying to use to cut it in half ... I
get errors.
I can display the normal picture but not the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |