Created
July 14, 2009 02:38
-
-
Save gabrielfalcao/146662 to your computer and use it in GitHub Desktop.
Metaclass example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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