Meaning of using commas and underscores with Python assignment operator? Meaning of using commas and underscores with Python assignment operator? python python

Meaning of using commas and underscores with Python assignment operator?


d2, = values[s] is just like a,b=f(), except for unpacking 1 element tuples.

>>> T=(1,)>>> a=T>>> a(1,)>>> b,=T>>> b1>>> 

a is tuple, b is an integer.


_ is like any other variable name but usually it means "I don't care about this variable".

The second question: it is "value unpacking". When a function returns a tuple, you can unpack its elements.

>>> x=("v1", "v2")>>> a,b = x>>> print a,bv1 v2


The _ in the Python shell also refers to the value of the last operation. Hence

>>> 11>>> _1

The commas refer to tuple unpacking. What happens is that the return value is a tuple, and so it is unpacked into the variables separated by commas, in the order of the tuple's elements.