meteclasses 2.x/3.x compatibility

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Apr 20 05:56:58 EDT 2011


On Wed, 20 Apr 2011 11:56:35 +1000, James Mills wrote:

> Hi all,
> 
> Is there a compatible way to use meteclasses in both Python 2.x (2.6 to
> 2.7) and Python 3.x (3.0 to 3.2).


Untested:

if sys.version >= "3":
    kw = {'metaclass': MyMetaClass}
else:
    kw = {}

class Foo(object, **kw):
    if sys.version < "3":
        __metaclass__ = MyMetaClass


Inelegant as it gets, but it should work.



-- 
Steven



More information about the Python-list mailing list