[英]Python's equivalence of using namespace
I have the following Helper.py python script 我有以下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'
I created this helper file to make code more readable. 我创建了这个帮助文件,使代码更具可读性。 So one can write this; 所以可以写这个;
# 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
The helper script is imported in many python files! 辅助脚本在许多python文件中导入! And the only way i know how to use it is as such 我知道如何使用它的唯一方法就是这样
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
Is there a way to get rid of the 'Helper.' 有没有办法摆脱'助手'。 I'm already adding more typing. 我已经添加了更多打字。 So it'll look like; 所以它看起来像;
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
you should just delete the wrapper Helper class and dedent the rest of the file ... the file itself provides the namespace if you want it 你应该只删除包装器Helper类并对文件的其余部分进行删除...如果你需要,文件本身就会提供命名空间
Helper.py Helper.py
class CMD_AA:
...
main.py main.py
import Helper
Helper.CMD_AA
# or just
from Helper import CMD_AA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.