Create a gist now

Instantly share code, notes, and snippets.

@Fak3 /
Last active Oct 8, 2015

What would you like to do?
Choices without metaclass
import inspect
class Choice(object):
def __init__(self):
self._data = []
for name, value in inspect.getmembers(self):
if not name.startswith("_") and not inspect.isfunction(value):
if isinstance(value,tuple) and len(value) > 1:
data = value
data = (value, " ".join([x.capitalize() for x in name.split("_")]),)
setattr(self, name, data[0])
def __iter__(self):
for value, data in self._data:
yield value, data
class Foo(Choice):
A = 1
B = 2
C = 3, 'CEEEE'
print 'list(Foo()) :', list(Foo())
# [(1, 'A'), (2, 'B'), (3, 'CEEEE')]
print 'Foo().C :', Foo().C
# 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment