Skip to content

Instantly share code, notes, and snippets.

@arsatiki
Created February 18, 2009 08:52
Show Gist options
  • Save arsatiki/66262 to your computer and use it in GitHub Desktop.
Save arsatiki/66262 to your computer and use it in GitHub Desktop.
Do not use functions as metaclasses. It makes inheritance super-painful.
# 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