Assigning an Empty List

I donโ€™t know how I came across this, and I donโ€™t know what to think about it, but it seems to [] = []be a legal operation in python, therefore [] = '', but '' = []not allowed. It doesn't seem to have any effect, but I wonder: what the hell?

+4
source share
2 answers

This is due to Python's multiple purpose (sequence unpacking):

a, b, c = 1, 2, 3

works the same as:

[a, b, c] = 1, 2, 3

Since strings are sequences of characters, you can also do:

a, b, c = "abc"    # assign each character to a variable

, , : . , , . ; !

, , Python :

() = ()            # SyntaxError: can't assign to ()

, Python !

+3

/ python, . .

>>> [a,v] = [2,4]
>>> print a
2
>>> print v
4
+1

Source: https://habr.com/ru/post/1655500/


All Articles