我正在为Python脚本创建一个接口。 稍后我将使用Python Python脚本进行自动化测试。 是否有必要我必须在我的代码中使用类。现在我已经创建了代码 使用词典,列表,函数,全局和局部变量。
是否需要上课?
帮助我。
答案 0 :(得分:10)
不,当然课不是必须。由于Python是一种脚本语言,您只需编写脚本代码而无需定义自己的类。 如果你实现一个需要结构化方法的更复杂的程序,并且OOP benfits(封装,polimorphism)可以帮助你完成它,那么类很有用。
答案 1 :(得分:1)
不需要让它工作,但我认为如果不在类中封装某些东西,维护会变得很混乱。类可以帮助程序员组织他/她的代码,而不仅仅是为了让他们感觉不舒服。
答案 2 :(得分:1)
不,您不需要使用类来编写脚本。
但是,当您开始使用单元测试框架unittest时,这将涉及到类,因此您至少需要了解如何对TestCase类进行子类化,例如:
import unittest
import os
class TestLint(unittest.TestCase):
def testLintCreatesLog(self):
# stuff that does things to create the file lint.log removed...
assert os.path.exists('lint.log') # this should be here after lint
assert os.path.getsize('lint.log') == 0 # nothing in the log - assume happy
if __name__ == '__main__':
# When this module is executed from the command-line, run all its tests
unittest.main()
答案 3 :(得分:0)
没有必要,因为python不是纯粹的面向对象语言,但某些东西更好地编写在类(封装)中。使用类构建大型项目变得更容易