我有一个项目,在开始之前。我想采纳专家的建议。
以下是项目要求:
I have to write three different scripts say script 1, script 2, script 3. Few of the variable values are same for three different scripts. So I planned include one more script say common-script so all the common variable values are declared in common-script.
- Script 1 may need a different variables values those are not required to script 2 and script 3 - similarly for script 2 and script 3.
以下是我的问题。
Shall I include only common variables values in common-script and declare non common variables in their script itself (Script 1 specific variables declare in script 1 itself not in common-script and similarly for script 2 and script 3).
Or shall I declare all the inputs and variables of the script 1, script 2, and script 3 in common-script file itself and then call variables values to scripts 1, 2 and 3?
哪个选项是最好的或者我需要遵循的任何其他标准?
The other requirement is I have to ask user to run all the scripts that is script 1 followed by script 2, script 3 or he want to run only specific script that is, he/she get options to select script 1, script 2, script 3. What is the best practices to implement these kind?
Still now I used only functions and write the separate scripts and import as modules. For these project first time I am planning to use python classes.
我需要您关于如何在该项目中使用类的提示,但我现在对这些类一无所知。但我开始阅读文档。
最佳答案
这个问题与面向对象编程类似:你可以有一个定义一些属性的基类和继承基类并重新定义属性的子类。由您选择。
但是,您所做的是一种配置文件。
我认为最佳实践是在公共(public)脚本中定义所有公共(public)变量。这样您就可以将它们全部记录在一个地方。这样比较好维护。
其他脚本只能重新定义它们需要的内容。
common.py
#: This is my var 1
VAR1 = "thing1"
#: This is my var 2
VAR2 = "thing2"
script1.py
from .common import *
VAR2 = "another thing2"
关于python - 为三种不同脚本编写 Python 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44330435/