Created
February 18, 2009 08:52
-
-
Save arsatiki/66262 to your computer and use it in GitHub Desktop.
Do not use functions as metaclasses. It makes inheritance super-painful.
This file contains 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 Foo(object): | |
# ... | |
# is syntactic sugar for | |
# Foo = type('Foo', (object,), {...}) | |
# And with a metaclass Meta: | |
# Bar = Meta('Bar', (object,), {...}) | |
def metafunc(name, bases, dict): | |
print "I created a new class %s%s from %s" % (name, bases, dict) | |
return type(name, bases, dict) | |
class A(object): | |
__metaclass__=metafunc | |
# I created a new class A(<type 'object'>,) from {'__module__': '__main__', '__metaclass__': <function metafunc at 0x258a70>} | |
class B(A, object): | |
__metaclass__=metafunc | |
an_attribute = "I am an attribute!" | |
# I created a new class B(<class '__main__.A'>, <type 'object'>) from {'an_attribute': 'I am an attribute!', '__module__': '__main__', '__metaclass__': <function metafunc at 0x258a70>} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment