I want to initialise a User
class with attributes whose value is taken from a finite and immutable set of possible values (e.g., types of user, short list of countries...). What is the best approach to do that in Python?
class User(object):
def __init__(self, type, country):
self.type = type # Possible values: [Consumer, Producer]
self.country = country # Possible values: [UK, USA, Japan, France]
It is probably obvious, but the reason for me to formally limit to this set of possible values is mainly to avoid/spot errors.
I have looked at various explanations on list
and Enum
in Python (e.g., How to implement an Enum in Python), but I am not sure if it is the right way to go considering my needs. Especially, it seems – from what I have read – that Enum
is to store a list of constants (User.type
as CONSUMER
and PRODUCER
)... but I would like to be able to use the values directly in the output (so the uppercase seems weird). And I am not sure whether a numeric equivalent for each value (CONSUMER=1
...) would be of much use for me.
EDIT: I should probably add that the real values in my application are in French and include thus non-ASCII characters (e.g., États-Unis
). Going the Enum
way seems to make it impossible to keep these characters and would mean to then "translate" the value to a "localised" value, which looks to me like a lot of trouble for a short list.