访问静态字典我有一个Enum类罗盘方向的如下。 我也有一个在同一类中声明的'对立'字典。在Python 3.6
from enum import Enum
class Compass(Enum):
N = 'N' # North
S = 'S' # South
E = 'E' # East
W = 'W' # West
opposites = {N: S, S: N, E: W, W: E}
# static method to provide the opposite values.
@staticmethod
def other(com):
return opposites[com]
当我试图打电话给其他人,例如。 Compass.other(Compass.N),我期望能获得Compass.S,而是我得到..
TypeError: 'Com' object is not subscriptable
这是怎么回事,我怎么能解决这个问题pythonically?
这是太酷了。我从这个答案中学到了比过去几天更多的Python。 – Konchog
@Konchog:好评!谢谢! –