My previous question asked about Enum in python
Lets say I am creating State classes:
class State(object):
"""
Abstract class representing a state.
"""
enum = 1
def __init__(self, instance):
self.instance = instance
def is_flag(self, flag):
return flag & self.enum == self.enum
And then I create different actual States
class New(State):
enum = 2
def pay(self):
...
class Paid(State):
enum = 4
def complete(self):
...
class Completed(State):
enum = 8
ACCOUNTABLE = Paid.enum | Completed.enum
while this works, I would like to automate the generation of the enum values, and it seems like it can be done by using Meta classes, the question is how?
ABCMetais unnecessary, both here and in the answer you posted to your own question. In Python abstract base classes aren't the same thing as they are in, say, C++. If you insist on using classes to represent different Enum values -- a questionable approach -- a metaclass is probably all you'd need.ABCMetacould stop accidentally instatingStateclass. I have also noticed the possible inconsistent enum values being generated, so in my actual implementation I have commented out the metaclass and specify the enum explicitly.