Although Enums are available in Python, I just like the idea or am used to using Classes instead which allows me to have more functionality in future if I need to attach additional methods to the class. Here’s how I have typically created a Class to store AWS Regions and have a method all() that would return all the region values – basically hard-coded.
class AwsRegion(): ''' Class to define AWS Regions ''' OHIO = 'us-east-2' NORTH_VIRGINIA = 'us-east-1' NORTH_CALIFORNIA = 'us-west-1' OREGON = 'us-west-2' MUMBAI = 'ap-south-1' SEOUL = 'ap-northeast-2' SINGAPORE = 'ap-southeast-1' SYDNEY = 'ap-southeast-2' TOKYO = 'ap-northeast-1' FRANKFURT = 'eu-central-1' IRELAND = 'eu-west-1' LONDON = 'eu-west-2' SAO_PAULO = 'sa-east-1' @classmethod def all(cls, ): return [ AwsRegion.OHIO, AwsRegion.NORTH_VIRGINIA, AwsRegion.NORTH_CALIFORNIA, AwsRegion.OREGON, AwsRegion.MUMBAI, AwsRegion.SEOUL, AwsRegion.SINGAPORE, AwsRegion.SYDNEY, AwsRegion.TOKYO, AwsRegion.FRANKFURT, AwsRegion.IRELAND, AwsRegion.LONDON, AwsRegion.SAO_PAULO ]
But I wanted to make the method all() more dynamic so every time I add new regions to the class, I don’t have to add the region to the all method return list. I found a better way to implement the all() method.
@classmethod def all(cls, ): return [value for name, value in vars(cls).items() if not name.startswith('__') and not callable(getattr(cls,name))]
The original stack overflow post.