Enums in Python: How to enforce in method arguments
Python is dynamic and duck typed - variables can change type and you can't force types on methods.
You can, however, check for types in the body of a method using isinstance()
.
- See commentsisinstance()
will allow users to subclass your enum
for future extensibility.
E.g.
# Python 2.x: pip install enum34
from enum import Enum
class Direction(Enum):
LEFT = "left"
RIGHT = "right"
UP = "up"
DOWN = "down"
def move(direction):
# Type checking
if not isinstance(direction, Direction):
raise TypeError('direction must be an instance of Direction Enum')
print direction.value
>>> move(Direction.LEFT)
left
>>> move("right")
TypeError: direction must be an instance of Direction Enum
The "pythonic" thing to do is to follow the principle of duck-typing: Try to accept the value you are passed without making too big of a fuss. In this case, instead of enforcing the type I would simply check for equality with each enum value, and raise an error for anything that cannot be handled:
def navigate(direction):
"""Turn toward `direction` (an enum of type `Direction`)"""
if direction == Direction.left:
print("Left")
elif direction == Direction.right:
(etc., etc.)
else:
# Hmm, `direction` does not compare equal to any enum value:
raise ValueError("Invalid direction "+ str(direction))