Why is this python line not an error?

why is the next python line not a syntax error? If it really isn't, then how can I use it and in what situation would it be useful?

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

      

+3


source to share


3 answers


Python supports multiple assignments on one line:

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

      

Since you are assigning the first list (a mutable object in python) and then assigning both sides of that list (which is mutable) and a is the content of the third element, python is fine with it. If you replaced your mean with something that was not volatile, 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 string like this still throws an error because you are trying to assign 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 is not the case:

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

      

The key in this case is that with slice notation, you are assigning a range within the list, rather than reassigning the literal values ​​in the list.

Also, the lines



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

      

and

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

      

Indeed, the first is because the inner lists and dicts are mutable and repeatable, and the second is because you are assigning 3 and 4 to b and c instead of literals (thanks Sven: D).

Edit . The answer to the final question.

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

      

It just boils down to the final (right) assignment to b, a = a, b. Note:

>>> 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'

      

This is confusing, but the way I interpret it is that a and b don't undergo reevaluation / assignment of new values ​​until the statement is complete, so the assignment that matters is the farthest to the right. If the last statement is a, b = a, b, or b, the values ​​for a = b, a, a, and b do not change. If it is a, b = b, a or b, a = a, b, the values ​​are toggled.

+6


source


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

a = [2, 3]

      

Also, it creates a new list with content [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and sets the second and third elements of that list to 2

and 3

finally discards this new list again since there are no links to it at all.



Here is more complete code to do the same. The order of the statements is chosen to match the order in which your original statement is executed:

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

      

+2


source


Well, let's break it down a bit starting with the range function. The range function creates a list of integers from 0 to 9. So you get this

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

      

Fragment notation is then applied to the list of integers. Putting a number in front of the colon tells python that you want to use it as the starting index, and also because lists are zero-indexed, you get the second item in the list, which is value 1. You also provided a second value that needs to be provided with an additional index. so you will get values ​​up to that index. But this only leads to the third element, which immediately follows the initial index value. You now have this:

[1,2]

      

Traditionally in programming languages, values ​​are assigned from right to left. It looks like the range / slice operations completed before the assignment because the right-most list replaces the value created by the range operation. Then the value is assigned to the value again.

Never do a dual purpose this way unless you have to. It is terrible to read and understand. Better to have readable / simpler code. I never had to ask anything. And to be honest, there was no reason to even handle the range operation because it was just replaced anyway. It's just an oddity that's gone. IMO.

+2


source







All Articles