python - 为三种不同脚本编写 Python 的最佳实践

标签 python python-2.7

我有一个项目,在开始之前。我想采纳专家的建议。

以下是项目要求:

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.

  1. 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.

以下是我的问题。

  1. 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).

  2. 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?

哪个选项是最好的或者我需要遵循的任何其他标准?

  1. 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?

  2. 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/

相关文章:

python - 为什么这个 Python 脚本会崩溃?

python - 绕过 Python Scapy 中的一些数据包层

python - 过滤值低于 0 的 pandas dataframe 行

python - xlwings for Excel for Mac 2016

python - python字典中的多个键,可能吗?

python-2.7 - Python读取unicode文件名

python - 实例化 python2 类并在另一个模块中使用的正确方法是什么

python - 如何在 Google App Engine 模板系统中注册自定义过滤器?

python - 强制从python中删除windows目录

python - 简单的 python 2.7 代码有某种问题 : "' list' object has no attribute 'find' "