Add a port of six.with_metaclass
@@ -381,6 +381,37 @@ else:
exec("""exec _code_ in _globs_, _locs_""")
+# this is six.with_metaclass, with a clearer docstring
+def with_metaclass(meta, *bases):
+ """
+ This provides:
+
+ class Foo(with_metaclass(Meta, Base1, Base2)): pass
+
+ which is a unifying syntax for:
+
+ # python 3
+ class Foo(Base1, Base2, metaclass=Meta): pass
+
+ # python 2
+ class Foo(Base1, Base2)
+ __metaclass__ = Meta
+
+ """
+ # This requires a bit of explanation: the basic idea is to make a dummy
+ # metaclass for one level of class instantiation that replaces itself with
+ # the actual metaclass.
+ class metaclass(type):
+
+ def __new__(cls, name, this_bases, d):
+ return meta(name, bases, d)
+
+ @classmethod
+ def __prepare__(cls, name, this_bases):
+ return meta.__prepare__(name, bases)
+ return type.__new__(metaclass, 'temporary_class', (), {})
+
+
if __name__ == "__main__":
import random
a = ""
