This is a Python 3.x version of the How to pass arguments to the metaclass from the class definition? question, listed separately by request since the answer is significantly different from Python 2.x.
In Python 3.x, how do I pass arguments to a metaclass's __prepare__
, __new__
, and __init__
functions so a class author can give input to the metaclass on how the class should be created?
As my use case, I'm using metaclasses to enable automatic registration of classes and their subclasses into PyYAML for loading/saving YAML files. This involves some extra runtime logic not available in PyYAML's stock YAMLObjectMetaClass
. In addition, I want to allow class authors to optionally specify the tag/tag-format-template that PyYAML uses to represent the class and/or the function objects to use for construction and representation. I've already figured out that I can't use a subclass of PyYAML's YAMLObjectMetaClass
to accomplish this--"because we don't have access to the actual class object in __new__
" according to my code comment--so I'm writing my own metaclass that wraps PyYAML's registration functions.
Ultimately, I want to do something along the lines of:
from myutil import MyYAMLObjectMetaClass
class MyClass(metaclass=MyYAMLObjectMetaClass):
__metaclassArgs__ = ()
__metaclassKargs__ = {"tag": "!MyClass"}
...where __metaclassArgs__
and __metaclassKargs__
would be arguments going to the __prepare__
, __new__
, and __init__
methods of MyYAMLObjectMetaClass
when the MyClass
class object is getting created.
Of course, I could use the "reserved attribute names" approach listed in the Python 2.x version of this question, but I know there is a more elegant approach available.
Here's the simplest way to pass arguments to a metaclass in Python 3:
Python 3.x
You can also create a base class for metaclasses that only use
__init__
with extra arguments:After digging through Python's official documentation, I found that Python 3.x offers a native method of passing arguments to the metaclass, though not without its flaws.
Simply add additional keyword arguments to your class declaration:
...and they get passed into your metaclass like so:
You have to leave
kargs
out of the call totype.__new__
andtype.__init__
(Python 3.5 and older; see "UPDATE" below) or will get you aTypeError
exception due to passing too many arguments. This means that--when passing in metaclass arguments in this manner--we always have to implementMyMetaClass.__new__
andMyMetaClass.__init__
to keep our custom keyword arguments from reaching the base classtype.__new__
andtype.__init__
methods.type.__prepare__
seems to handle the extra keyword arguments gracefully (hence why I pass them through in the example, just in case there's some functionality I don't know about that relies on**kargs
), so definingtype.__prepare__
is optional.UPDATE
In Python 3.6, it appears
type
was adjusted andtype.__init__
can now handle extra keyword arguments gracefully. You'll still need to definetype.__new__
(throwsTypeError: __init_subclass__() takes no keyword arguments
exception).Breakdown
In Python 3, you specify a metaclass via keyword argument rather than class attribute:
This statement roughly translates to:
...where
metaclass
is the value for the "metaclass" argument you passed in,name
is the string name of your class ('MyClass'
),bases
is any base classes you passed in (a zero-length tuple()
in this case), andkargs
is any uncaptured keyword arguments (an emptydict
{}
in this case).Breaking this down further, the statement roughly translates to:
...where
kargs
is always thedict
of uncaptured keyword arguments we passed in to the class definition.Breaking down the example I gave above:
...roughly translates to:
Most of this information came from Python's Documentation on "Customizing Class Creation".
It's worth to say, that this style is not backward compatible to python 2. If you want to support both python 2 and 3, you should use:
Here's a version of the code from my answer to that other question about metaclass arguments which has been updated so that it'll work in both Python 2 and 3. It essentially does the same thing that Benjamin Peterson's six module's
with_metaclass()
function does — which namely is to explicitly create a new base class using the desired metaclass on-the-fly, whenever needed and thereby avoiding errors due to the metaclass syntax differences between the two versions of Python (because the way to do that didn't change).Output: