3

why the next python line is not a syntax error? If it is really not then how can I use it and in which situation it will be useful?

 a= range(10)[1:3]=[2,3]
4

3 に答える 3

6

Python supports multiple assignments on the same line:

>>> a = b = c = 2
>>> a
2
>>> b
2
>>> c
2

Since you are assigning a first to a list(a mutable object in python) and then assigning both part of that list(which is mutable) and a to the contents of a third item, python is ok with it. If you had replaced your middle value with something that wasn't mutable, you would get an error.

>>> a = 'foo' = 'bar'
  File "<input>", line 1
SyntaxError: can't assign to literal

>>> a = {}['foo'] = 'bar'
>>> a
'bar'

Interestingly, a line like this still throws an error, because you are attempting to assign the values 3 and 4 to the literal values 1 and 2.

>>> a = [1, 2] = [3, 4]
  File "<input>", line 1
SyntaxError: can't assign to literal

While this does not:

>>> a = [1, 2][:] = [3, 4]
>>> a
[3, 4]

The key in this case is that by using slice notation you are assigning to the range inside of the list, instead of reassigning to the literal values in the list.

Additionally, the lines

>>> a = [[], []] = [{}, {}]
>>> a
[{}, {}]

and

>>> a = [b, c] = 3, 4
>>> b
3
>>> c
4

Are valid, the first one because the inner lists and dicts are both mutable and iterable, and the second one because you are assigning 3, and 4 to the variables b and c instead of literals(thanks Sven :D).

Edit To answer the op's final question.

>>> a = 1
>>> b = 2
>>> a,b = b,a = a,b
>>> a
2
>>> b
1

This just comes down to the final(right-most) assignment of b, a = a, b. Observe:

>>> a, b = 'a', 'b'
>>> a, b = b, a = a, b = b, a
>>> a
'b'
>>> b
'a'
>>> a, b = b, a = b, a
>>> a
'a'
>>> b
'b'

It's confusing, but the way I interpret this is that a and b are not re-evaluated / assigned new values until the statement is finished, so the assignment that matters is the one farthest to the right. If the last statement is a, b = a, b or b, a = b, a, a and b's values don't change. If it is a, b = b, a or b, a = a, b, the values are switched.

于 2012-04-06T23:39:31.210 に答える
2

This line is indeed not an error, but it is never useful. It does the same as

a = [2, 3]

Additionally, it creates a new list with the contents [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], and sets the second and third item of this list to 2 and 3, and finally drops this new list again since there are no references at all to it.

Here is a more complete code doing the same. The order of the statements is chosen to match the execution order of your original statement:

a = [2, 3]
b = range(10)
b[1:3] = a
del b
于 2012-04-06T23:35:18.903 に答える
2

Well let's break this down a bit starting with the range function. The range function creates a list of integers from 0 - 9. So you end up with this

[0,1,2,3,4,5,6,7,8,9]

Next a slice notation is applied to your list of integers. Specifying a number before the colon tells the python that you want to use that as your starting index and because lists are zero indexed, you get the second element in the list which is the value 1. You also gave a second value the number to provide an additional index, so you will get values up to that index. But that only results in the third element, which directly follows the start index value. Now you have this:

[1,2]

Traditionally in programming languages values are assigned right to left. It appears that the range/slice operations complete before assignment because the far right list replaces the value created by the range operation. Then the value is assigned again to a.

Don't ever double assign like this unless you absolutely have to. This is horrible to read and understand. It's better to have readable/simpler code. I've never had to assign something this way. And honestly, there was no reason to even process the range operation because it was just replaced anyhow. It's just an oddity best left unused. IMO.

于 2012-04-06T23:41:57.137 に答える