First of, I know Enum in Python are immutable.
I'm trying to get an enum that has each month as an attribute and also other enum values, for example:
class SalesFilter(str, enum.Enum): this_month = 'this_month' last_month = 'last_month' this_year = 'this_year' last_year = 'last_year' jan = 'jan' feb = 'feb' mar = 'mar' apr = 'apr' may = 'may' jun = 'jun' jul = 'jul' aug = 'aug' sep = 'sep' oct = 'oct' nov = 'nov' dec = 'dec' I wonder if there's a way to generate those months dynamically, I'd like to do something like:
import calendar months = [m.lower() for m in list(calendar.month_abbr)[1:]] class SalesFilter(str, enum.Enum): this_month = 'this_month' last_month = 'last_month' this_year = 'this_year' last_year = 'last_year' for m in months: setattr(SalesFilter, f'{m}', m) Is there a way to do that? Maybe with metaclasses, but I'm not sure where to start.
SalesFilterclass after defining the basic class — i.e. after and outside theclassdefinition.