Skip to content

Instantly share code, notes, and snippets.

@gabrielfalcao
Created July 14, 2009 02:38
Show Gist options
  • Save gabrielfalcao/146662 to your computer and use it in GitHub Desktop.
Save gabrielfalcao/146662 to your computer and use it in GitHub Desktop.
Metaclass example
class WooMeta(type):
def __init__(cls, name, bases, attrs):
names = [n for n in attrs.keys() if not n.startswith('_')]
cls._attributes = tuple(names)
super(WeeMeta, cls).__init__(name, bases, attrs)
class Wee(object):
__metaclass__ = WooMeta
foo = 1
zoo = 2
class Person(Wee):
name = "john"
age = 10
assert Wee._attributes == ('foo', 'zoo'), \
'Expected tuple("foo", "zoo"), got %s' % repr(Wee._attributes)
assert Person._attributes == ('age', 'name'), \
'Expected tuple("age", "name"), got %s' % repr(Person._attributes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment