Python 具有常量继承的类
对于Python中的gpib设备接口(使用ctypes模块),我将该设备在类中使用的常量分组:Python 具有常量继承的类,python,inheritance,Python,Inheritance,对于Python中的gpib设备接口(使用ctypes模块),我将该设备在类中使用的常量分组: class IbcnfOPTConst(object): ''' The following constants are used for the second parameter of the ibconfig() function. They are the "option" selection codes. ''' IbcPAD
class IbcnfOPTConst(object):
'''
The following constants are used for the second parameter of the ibconfig() function.
They are the "option" selection codes.
'''
IbcPAD = 0x0001 #// Primary Address
IbcSAD = 0x0002 #// Secondary Address
IbcTMO = 0x0003 #// Timeout Value
IbcEOT = 0x0004 #// Send EOI with last data byte?
...
另一个函数需要几乎相同的常量,但更新为新常量,而旧常量现在具有不同的含义,因此我将新类定义为现有类的继承:
class IbAskConst(IbcnfOPTConst):
''' Constants that can be used
when calling the ibask() function.
'''
IbaPAD = IbcnfOPTConst.IbaSAD
IbaTMO = IbcnfOPTConst.IbcTMO
IbaEOT = IbcnfOPTConst.IbcEOT
IbaPPC = IbcnfOPTConst.IbcPPC
....
IbaSerialNumber = 0x0023
IbaBNA = 0x0200 #// A device's access board
关于继承,有没有更好的方法在子类中使用常量 我已经在这里回答了,但我意识到这个问题可能已经发布到了。这是当你有一个完全可以工作的代码需要改进时,你通常应该发布的地方,StackOverflow是针对代码的问题。你是添加了最后两个常量还是更改了它们?此外,子级的主地址实际上是父级的辅助地址,还是输入错误?在ni4882.h中,定义了://ibconfig常量:#定义IbcPAD 0x0001//主地址#定义IbcSAD 0x0002//ibask()的辅助地址//常量#定义IbaPAD IbcPAD#定义IbaSAD IbcSAD#定义IbaSerialNumber 0x0023,以便从此处使用基类和派生类定义IDEA(仅意味着第二个函数使用重命名的常量)