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)
Intended output is supposed to be
Direction.EAST
Direction.SOUTH
Direction.WEST
after each turn, but what I got instead was just
Direction.EAST
Direction.EAST
Direction.EAST
Looks like it's not updating self
, but why? How can I change the class such that its usage stays the same?
One possible workaround is
def turn_right(self):
return Direction((self.value + 1) % 4)
but this returns a new Direction
everytime, is there a way to make the method work "in-place"?