Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign function's output to empty list constant in python [duplicate]

I have found something like this in a code I'm working with:

[], my_variable = my_function(a, b)

where the output of my_function goes like:

return some_dict, some_list

This seems to work - unit tests of a system don't fail, but when I try this at the python console (assigning dictionary to "[]") it raises:

ValueError: too many values to unpack

Have you seen something like this? How does assigning a dictionary (or something else) to empty list constant "[]" work?

Or it doesn't and tests are missing something...

like image 260
Gwidryj Avatar asked Oct 19 '22 02:10

Gwidryj


1 Answers

It's because in your test file, the return value is {}.

>>> [] = {}
>>> [] = {1:2}
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: too many values to unpack


>>> [] = []
>>> [] = [1,2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
like image 96
luoluo Avatar answered Oct 21 '22 16:10

luoluo