Skip to content
Advertisement

How to pass arguments to the metaclass from the class definition?

I’m trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object.

I’ve read this post, which is awesome, but doesn’t quite answer the question. at the moment I am doing:

JavaScript

but this requires me to do the following

JavaScript

Is there a cleaner way of doing this?

Advertisement

Answer

Yes, there’s an easy way to do it. In the metaclass’s __new__() method just check in the class dictionary passed as the last argument. Anything defined in the class statement will be there. For example:

JavaScript

Output:

JavaScript

Update

The code above will only work in Python 2 because syntax for specifying a metaclass was changed in an incompatible way in Python 3.

To make it work in Python 3 (but no longer in Python 2) is super simple to do and only requires changing the definition of MyClass to:

JavaScript

It’s also possible to workaround the syntax differences and produce code that works in both Python 2 and 3 by creating base classes “on-the-fly” which involves explicitly invoking the metaclass and using the class that is created as the base class of the one being defined.

JavaScript

Class construction in Python 3 has also been modified and support was added that allows other ways of passing arguments, and in some cases using them might be easier than the technique shown here. It all depends on what you’re trying to accomplish.

See @John Crawford’s detailed answer for a description of of the process in the new versions of Python.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement