Skip to content

Instantly share code, notes, and snippets.

@mjhennig
Created October 6, 2015 08:36
Show Gist options
  • Select an option

  • Save mjhennig/22a16f34b9d3317c06c7 to your computer and use it in GitHub Desktop.

Select an option

Save mjhennig/22a16f34b9d3317c06c7 to your computer and use it in GitHub Desktop.
Metaclasses in Python 2 and 3
def with_metaclass(metaclass, *bases):
"""
Resembles functions six.with_metaclass() and flask.with_metaclass().
"""
constructor = lambda c, name, b, dct: metaclass(name, bases, dct)
intermediate = type('__metaclass__', (type,), {'__new__': constructor})
return type.__new__(intermediate, '__class__', (object,), {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment