为什么不在字典中把boolens理解为函数调用呢



我有以下

class ShowOptions(Enum):
MenuBBoxes =0
Show_A = 1
Show_B =2
Show_C =3
Show_D =4
isShown = { ShowOptions.MenuBBoxes: True,
ShowOptions.Show_A: True,
ShowOptions.Show_B:True,
ShowOptions.Show_C:True,
ShowOptions.Show_D:True}

我正在尝试切换布尔值,例如

isShown[(h-1)]= not   isShown[(h-1)]

但这给了我错误

TypeError: 'NoneType' object is not callable

我不明白为什么不能用。注意:如果我做

isShown[(h-1)]= not (1==0)

没有问题

dict的键是Enum而不是int。它们不能互换使用。如果你想从dict访问一些东西,你需要使用Enum的定义,比如:

from enum import Enum

class ShowOptions(Enum):
MenuBBoxes = 0
Show_A = 1
Show_B = 2
Show_C = 3
Show_D = 4

isShown = {ShowOptions.MenuBBoxes: True,
ShowOptions.Show_A: True,
ShowOptions.Show_B: True,
ShowOptions.Show_C: True,
ShowOptions.Show_D: True}

isShown[ShowOptions.MenuBBoxes] = not isShown[ShowOptions.MenuBBoxes]
print(isShown)

如果你想使用它们的值,你必须调用你的枚举类型,比如:

isShown[ShowOptions(0)] = not isShown[ShowOptions(0)]
print(isShown)

我想你在找这样的东西:

isShown[ShowOptions(h - 1)] = not isShown[ShowOptions(h - 1)]

最新更新