Why does comprehension only work with tuples when unpacking in Python?

  Kiến thức lập trình

I tried to use list comprehension on a tuple and it worked fine when unpacking, but not when assigning to a single variable.

If I run the code

var1, var2, var3 = (i for i in range(3))

var1 = 1, var2 = 2, and var3 = 3 as expected. However, when running

varTuple = (i for i in range(3))

varTuple = <generator object <genexpr> at [MEMORY ADDRESS]>, instead of the expected (1, 2, 3)

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT