本站资源收集于互联网,不提供软件存储服务,每天免费更新优质的软件以及学习资源!

Python中AttributeError错误:为什么TestEmployee对象没有employee属性?

网络教程 app 1℃

Python中AttributeError错误为什么TestEmployee对象没有employee属性

python 中的 attributeerror 问题

在编写一个 python 程序时,可能会遇到 attributeerror。该错误表明尝试访问或操作对象中不存在的属性。

问题

以下代码展示了 employee.py 文件和主代码:

# employee.pyclass employee(): def __init__(self, first_name, last_name, pay): self.first_name = first_name.title() self.last_name = last_name.title() self.pay = pay def give_raise(self, pay_raise=5000): self.pay += pay_raise# 主代码import unittestfrom employee import employeeclass testemployee(unittest.testcase): def setup(self): self.employee = employee(‘taylor’, ‘swift’, 20000) def test_give_default_raise(self): self.employee.give_raise() self.assertequal(self.self.pay, 25000)unittest.main()

运行此代码后,会出现以下错误:

attributeerror: ‘testemployee’ object has no attribute ’employee’

解决方法

该错误是由于拼写错误造成的。setup 方法应更正为 setup,如下所示:

class TestEmployee(unittest.TestCase): def setUp(self): self.employee = Employee(‘taylor’, ‘swift’, 20000)

更正拼写后,代码应该可以正常运行,并且不会出现 attributeerror。

以上就是Python 中 AttributeError 错误:为什么 TestEmployee 对象没有 employee 属性?的详细内容,更多请关注范的资源库其它相关文章!

转载请注明:范的资源库 » Python中AttributeError错误:为什么TestEmployee对象没有employee属性?

喜欢 (0)