我刚开始使用Python中的类,我想我误解了一些东西。
我正在开发一个应用程序,允许用户运行一个小测试,以确保一切正常。这是代码
## main.py ##
# Import libraries
from classes.test.test_control import TestingControl
user_input = sys.argv[1]
if user_input == 'run_test':
# Run test control
TestingControl()
## test_control.py
class TestingControl:
def test_control_vcf()
...
# The class and the function don't need any argument
我真的需要一个类来做这件事吗,或者直接使用一个函数代替它是一个好的实践吗?
我已经阅读了下面的链接,但仍然不清楚是否在这个例子中我应该使用它
我什么时候应该在Python中使用类?
(我可以直接使用函数,这是工作,但(除了这将被评估)我想学习。
这取决于个人选择。我推荐观看Stop Writing Classes
byJack Diederich
, Jack是一个python核心开发人员。我附上了youtube视频的链接。最后,这是你的决定,无论你想要一个class
或function
是好的。
https://youtu.be/o9pEzgHorH0