for循环未打印所有定义的枚举
我定义了一个枚举(Enum),想要检查数据库1(db1)中的键和数据库2(db2)之间的对应关系。在用for循环遍历的时候,结果并没有打印出我定义的所有枚举值。
from enum import Enum
class SecData(Enum):
secId = "sectoId"
anteName = "anteName"
rAzimuth = "rAzimuth"
agl = "AboveGroundLevel"
actualAzimuth = "AntennaHeight"
actualLatitude = "SectorLat"
technology = "technology"
antennaType = "sectorAntennaType"
actualElectricalTilt = "sectorElecTilt"
actualMechanicalTilt = "sectorMechTilt"
sectorNumber = "sectorNum"
rtsTilt = "rtsTilt"
band = "sectorBand"
actualLongitude = "SectorLong"
actualAntennaheight = "sectorAntennaHeight"
siteReferenceId = "null"
retiid4 = "null"
retiid2 = "null"
retiid3 = "null"
NominalId = "null"
retiid1 = "null"
status = "null"
for sector in (SecData):
print(sector.name, "-" , sector.value)
输出结果
secId - sectoId
anteName - anteName
rAzimuth - rAzimuth
agl - AboveGroundLevel
actualAzimuth - AntennaHeight
actualLatitude - SectorLat
technology - technology
antennaType - sectorAntennaType
actualElectricalTilt - sectorElecTilt
actualMechanicalTilt - sectorMechTilt
sectorNumber - sectorNum
rtsTilt - rtsTilt
band - sectorBand
actualLongitude - SectorLong
actualAntennaheight - sectorAntennaHeight
siteReferenceId - null
有人能帮我找出这个脚本的问题吗?
2 个回答
1
来自文档
枚举是一组符号名称(成员),它们与唯一的值绑定在一起。
枚举的每个值应该是不同的。在你的情况下,你有多个空值。
所以其他的空值在你的代码中没有影响。
siteReferenceId = "null"
retiid4 = "null"
retiid2 = "null"
retiid3 = "null"
NominalId = "null"
retiid1 = "null"
status = "null"
1
所有值为“null”的成员其实是彼此的别名。
正如在文档开头所说:
一个枚举:
[...]
可以被遍历,返回其标准成员(也就是非别名成员),按照定义的顺序
所以,别名成员在遍历时实际上被合并成一个项目。这就是为什么你只看到siteReferenceId
被打印出来(第一个“null”成员)。
如果你想避免使用别名,可以使用验证装饰器和UNIQUE参数。