You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A suggestion would be to use Python Enum for constants, because they provide some small advantages over plain class constants. For example
from enum import Enum
class DIRECTION(Enum):
NORTH = "n"
WEST = "w"
SOUTH = "s"
EAST = "e"
CENTER = "c"
While they can be used similarly as in the previous version, small advantages - apart from being more pythonic - are
>>> list(DIRECTION) # possible to iterate; very useful sometimes
[<DIRECTION.NORTH: 'n'>,
<DIRECTION.WEST: 'w'>,
<DIRECTION.SOUTH: 's'>,
<DIRECTION.EAST: 'e'>,
<DIRECTION.CENTER: 'c'>]
>>> DIRECTION.NORTH.name # access to readable name for print output
'NORTH'
>>> DIRECTION.NORTH.value # access to raw string value, but there shouldn't be a reason, unless you process external output
'n'
Other advantages are that type-checkers like mypy will notice if you accidentally compare them against unrelated strings.
However, note that if for whatever reason you introduce raw strings instead of using the proper class, you will need to access the value with DIRECTION.NORTH.value (for example for if my_direction.value == "n":)
The text was updated successfully, but these errors were encountered:
A suggestion would be to use Python Enum for constants, because they provide some small advantages over plain class constants. For example
While they can be used similarly as in the previous version, small advantages - apart from being more pythonic - are
Other advantages are that type-checkers like mypy will notice if you accidentally compare them against unrelated strings.
However, note that if for whatever reason you introduce raw strings instead of using the proper class, you will need to access the value with
DIRECTION.NORTH.value
(for example forif my_direction.value == "n":
)The text was updated successfully, but these errors were encountered: