python中的NoneType错误

时间:2013-11-25 09:54:07

标签: python testing

我运行此代码,但我得到了typeError:'NoneType'对象不可迭代 什么是问题?

import bibtex
import unittest

class TestAuthorExtract(unittest.TestCase):
   def setUp(self):
       self.simple_author_1 = "Smith"

   def test_author_1(self):
       # Test only surname
       (Surname , FirstNames) = bibtex.extract_author(self.simple_author_1)
       self.assertEqual((Surname , FirstNames) , ("Smith", ""))

if __name__=='__main__':
    unittest.main()

以下是错误消息:

====================================================================== 
ERROR: test_author_1 (main.TestAuthorExtract)
----------------------------------------------------------------------
Traceback (most recent call last): 
  File "div.py", line 10, in test_author_1 
    (Surname , FirstNames) = bibtex.extract_author(self.simple_author_1) 
  TypeError: 'NoneType' object is not iterable
---------------------------------------------------------------------- 
Ran 1 test in 0.001s FAILED (errors=1)

1 个答案:

答案 0 :(得分:2)

这意味着,bibtex.extract_author()会返回None。之后,解释器尝试将值赋给元组(Surname, Firstname),这只能用长度为2的迭代(称为unpacking)来完成。