我有以下Helper.py python脚本
class Helper:
# Helper has many many classes! (AA, BB, CC, DD, ....) not just 2!
class AA:
CMD_AA = '8'
CMD_AB = '1'
class BB:
CMD_BA = '0'
CMD_BB = '1'
我创建了这个帮助文件,使代码更具可读性。所以可以写这个;
# this usage is more readable and makes sense
cmd.a = AA.CMD_AB
cmd.b = BB.CMD_BB
# instead of some thing like this where no one knows
# what 1 is interms of a or b!!
cmd.a = 1
cmd.b = 1
帮助程序脚本在许多python文件中导入!我知道如何使用它的唯一方法就是这样
from Helper import *
cmd.a = Helper.AA.CMD_AA # instead of cmd.a = 8
cmd.b = Helper.BB.CMD_BB # instead of cmd.b = 1
有没有办法摆脱帮助者。'我已经添加了更多打字。所以它看起来像;
from Helper import *
#in C++ u can use something like using namespace Helper;
cmd.a = AA.CMD_AA # instead of cmd.a = 8
cmd.b = BB.CMD_BB # instead of cmd.b = 1
答案 0 :(得分:3)
你应该删除包装器Helper类并删除文件的其余部分......如果你需要,文件本身就会提供命名空间
Helper.py
class CMD_AA:
...
main.py
import Helper
Helper.CMD_AA
# or just
from Helper import CMD_AA