python - 在 Python 中修改枚举类
问题描述
from enum import Enum
class Direction(Enum):
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
def turn_right(self):
self = Direction((self.value + 1) % 4)
d = Direction.EAST
print(d)
d.turn_right()
print(d)
d.turn_right()
print(d)
预期的输出应该是
Direction.EAST
Direction.SOUTH
Direction.WEST
每一回合之后,但我得到的只是
Direction.EAST
Direction.EAST
Direction.EAST
好像没有更新self
,为什么呢?如何更改类以使其用法保持不变?
一种可能的解决方法是
def turn_right(self):
return Direction((self.value + 1) % 4)
但这Direction
每次都会返回一个新的,有没有办法让该方法“就地”工作?
解决方案
推荐阅读
- python - 一个初学者制作的python数字猜谜游戏。请帮助优化和解决问题
- google-calendar-api - How can we filter the google calendar events with attendee name
- javascript - Webrtc adding ice candidate to remote peer
- javascript - Firefox 上的 HTTPS iFrame 出现 SecurityError
- c - 形式参数中有关 C 结构数组的错误
- java - 使用 java 10 apache tomcat 9 不是在 eclipse 中启动但它的服务是在系统中启动的
- java - Java中如何用子类的方法覆盖父类的方法?
- javascript - 只能看到 javascript 部分的代码,而不是网页上的 html
- loopbackjs - 如何使用middlewre在环回中使用会话
- liferay - javax.servlet-api-3.0.1.jar 未在 liferay 6.1 和 tomcat 7.0 中加载