我收到错误:ValueError:需要超过 2 个值才能解包 当我现在运行单元测试时,有 2 次失败和 1 次跳过 据我所知
lambda i: get_error_count(self._error_lookup, i))
line 142 of source is the method
for test, err, capt in errors:
which has the line of code:
count = get_error_count(i)
reference Python 3.0 has something a bit like this. Excess values can be bound (as a list) to the last variable:
a,b,*c = [1,2,3,4,5]
will result in c containing [3,4,5].
In Python 2.x, you can't do that directly, but you should be able to create a function that lengthens or shortens an input tuple of arguments to the correct length so you can do:
a,c,b = fix(1,2) d,e,f = fix(1,2,3,4)
However, the function won't know the length of the left hand side sequence, so it will have to be passed in as an extra parameter or hard coded.
so the
count = get_error_count(i) uses only one variable, where as def get_error_count(lookup, index): takes on 2
What should i use as the second variable ? to fix this ?
Thanks, -Kamal.
-------------------- >> begin captured stdout << ---------------------
\ test_many_errors.test_assert_one ... FAIL test_many_errors.test_one ... ok test_many_errors.test_assert_two ... ERROR test_many_errors.test_two ... ok test_many_errors.test_value_one ... ERROR test_many_errors.test_value_two ... SKIP: (, ValueError(), ) test_many_errors.test_good_one ... ok test_many_errors.test_good_two ... ok
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/Current/bin/nosetests", line 10, in <module>
sys.exit(run_exit())
File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/site-packages/nose/core.py", line 117, in __init__
**extra_args)
File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/unittest.py", line 817, in __init__
self.runTests()
File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/site-packages/nose/core.py", line 196, in runTests
result = self.testRunner.run(self.test)
File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/site-packages/nose/core.py", line 63, in run
result.printErrors()
File "/NOSE_TRIM/nosetrim-read-only/nosetrim/nosetrim.py", line 136, in printErrors
lambda i: get_error_count(self._error_lookup, i))
File "/NOSE_TRIM/nosetrim-read-only/nosetrim/nosetrim.py", line 142, in printErrorList
for test, err, capt in errors:
ValueError: need more than 2 values to unpack
/
-------------------->> 结束捕获的标准输出 << ------------------ ---
在 1.263 秒内运行 3 次测试
最佳答案
而不是在你的作业中解包:
a, b, c = do_something()
尝试将结果分配给单个变量并测试其长度:
t = do_something()
# t is now a tuple (or list, or whatever was returned) of results
if len(t) > 2:
# Can use the third result!
c = t[2]
关于python - 值错误 : need more than 2 values to unpack in Python 2. 6.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5515859/