Skip to content

Instantly share code, notes, and snippets.

@Fak3
Last active October 8, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fak3/3394633 to your computer and use it in GitHub Desktop.
Save Fak3/3394633 to your computer and use it in GitHub Desktop.
Choices without metaclass
import inspect
#y
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
else:
data = (value, " ".join([x.capitalize() for x in name.split("_")]),)
self._data.append(data)
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