Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
174244 Lượt xem

Can any one give me the cause of the above error message

Ảnh đại diện
Huỷ bỏ

Some context may help. Anyway, in python normally means you are trying to unpack a tuple with more values repect to target variables. Example: a,b = returnATupleOfMoreThan2Values()

Câu trả lời hay nhất

It is a Python exception that is the most ofen risen during assignation error: You try to do multiple assignment :

a, b = (1, 2, 3)  #  There is too many value to unpack ;)
a, b = (1, 2)  # That will work
a, b = 'base.main_company'.split('.')  # OK
a, b = 'base.main.company'.split('.')  # KO

In OpenERP it generaly comes when there is a dot in an XML ID. <record id='my.car' ... You should not use the dot when creating an XML ID. It can also be risen when you try to acces an item by his XML id and pass wrong parameters.

That the most common cases but it may also come from any other piece of code...

Regards

Nicolas

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Unpacking in Python:

  • Imagine a function that returns multiple results, like a grocery list with several items.
  • Unpacking allows you to assign each item on the list to a separate variable in your code.
  • It's like taking things out of a bag one by one and putting them in designated spots.

The Error:

  • The error pops up when you have more "spots" (variables) than items (returned values) or vice versa.
  • If there are fewer variables than returned values, you're missing spots for some items, and Python doesn't know where to put them.
  • On the other hand, if there are more variables than returned values, you have extra empty spots and not enough items to fill them all.


Ảnh đại diện
Huỷ bỏ

Python
def get_name_and_age():
return "Alice", 30 # Function returns a tuple with two values (name, age)

name, age, extra_variable = get_name_and_age() # Trying to unpack 3 values into 2 variables
https://slope3.io
In this example, the get_name_and_age function returns two values: "Alice" (name) and 30 (age). But the code tries to unpack these two values into three variables (name, age, and extra_variable). Since there's an extra variable, Python throws the "Too many values to unpack" error.

Câu trả lời hay nhất

ValueError is a standard Exception raised by various methods that perform range-checking of some kind to signal that a value provided to the method fell outside the valid range. Python functions can return multiple variables . These variables can be stored in variables directly. This is a unique property of Python , other programming languages such as C++ or Java do not support this by default. The valueerror: too many values to unpack occurs during a multiple-assignment where you either don't have enough objects to assign to the variables or you have more objects to assign than variables. 

 More info:    http://net-informations.com/python/err/value.htm  

 



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Another case may be regarding looping over dictionary. If one is looping on any dictionary with key and val both without using iteritems(), one will face this error:

a = {'test': 1, 'test 1': 2} 
for k, v in a:
    print k

It will give you that error but instead of one should use it like this:

a = {'test': 1, 'test 1': 2}
for k, v in a.iteritems():
    print k

Still more information can be useful!

Thanks, Priyesh Solanki

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 3 25
1364
0
thg 12 23
2153
5
thg 7 25
227969
1
thg 12 22
3266
2
thg 11 22
3200