With an Enum object as such:
from enum import Enum
class Fruits(str, Enum):
apple = "apple"
banana = "banana"
orange = "orange"
lemon = "lemon"
_citrus = {orange, lemon}
def is_citrus(self):
return self in self.citrus
when we try to check its member:
>>> Fruits.__members__.keys()
dict_keys(['apple', 'banana', 'orange', 'lemon', '_citrus'])
we get all possible Enum values. Is there a way to declare private vs public enums where some specific values are private and not accessible by Enum.__members__?
It's possible to do this:
from enum import Enum
class Fruits(str, Enum):
apple = "apple"
banana = "banana"
orange = "orange"
lemon = "lemon"
_citrus = {orange, lemon}
def is_citrus(self):
return self in self.citrus
@classmethod
def values(cls):
return set(k for k in cls.__members__.keys() if not k.startswith('_'))
Usage:
>>> Fruits.values()
{'apple', 'banana', 'lemon', 'orange'}
Are there some other way to have restricted members access?
Are there examples of private vs public members in Python's Enum?