{"id":2964,"date":"2021-11-17T01:38:21","date_gmt":"2021-11-17T01:38:21","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2964"},"modified":"2025-03-28T03:36:41","modified_gmt":"2025-03-28T03:36:41","slug":"python-metaclass","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass\/","title":{"rendered":"Python Metaclass"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python metaclass and understand how Python uses the metaclasses to create other classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-metaclass'>Introduction to the Python Metaclass <a href=\"#introduction-to-the-python-metaclass\" class=\"anchor\" id=\"introduction-to-the-python-metaclass\" title=\"Anchor for Introduction to the Python Metaclass\">#<\/a><\/h2>\n\n\n\n<p>A metaclass is a <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a> that creates other classes. By default, Python uses the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-type-class\/\">type<\/a><\/code> metaclass to create other classes.<\/p>\n\n\n\n<p>For example, the following defines a <code>Person<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When Python executes the code, it uses the <code>type<\/code> metaclass to create the <code>Person<\/code> class. The reason is that the <code>Person<\/code> class uses the <code>type<\/code> metaclass by default. <\/p>\n\n\n\n<p>The explicit <code>Person<\/code> class definition looks like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">(object, metaclass=type)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>metaclass<\/code> argument allows you to specify which metaclass class to use to define the class. Therefore, you can create a custom metaclass that has its own logic to create other classes. By using a custom metaclass, you can inject functionality into the class creation process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-metaclass-example'>Python metaclass example <a href=\"#python-metaclass-example\" class=\"anchor\" id=\"python-metaclass-example\" title=\"Anchor for Python metaclass example\">#<\/a><\/h2>\n\n\n\n<p>First, define a custom metaclass called <code>Human<\/code> that has the <code>freedom<\/code> attribute sets to <code>True<\/code> by default:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Human<\/span><span class=\"hljs-params\">(type)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__new__<\/span><span class=\"hljs-params\">(mcs, name, bases, class_dict)<\/span>:<\/span>\n        class_ = super().__new__(mcs, name, bases, class_dict)\n        class_.freedom = <span class=\"hljs-literal\">True<\/span>\n        <span class=\"hljs-keyword\">return<\/span> class_<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__new__\/\">__new__<\/a><\/code> method returns a new class or a class object.<\/p>\n\n\n\n<p>Second, define the <code>Person<\/code> class that uses the <code>Human<\/code> metaclass:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">(object, metaclass=Human)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Person<\/code>class will have the <code>freedom<\/code> attribute as shown in the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-variables\/\">class variables<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pprint(Person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mappingproxy({<span class=\"hljs-string\">'__dict__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__dict__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'__doc__'<\/span>: <span class=\"hljs-literal\">None<\/span>,\n              <span class=\"hljs-string\">'__init__'<\/span>: &lt;function Person.__init__ at <span class=\"hljs-number\">0x000001E716C71670<\/span>&gt;,\n              <span class=\"hljs-string\">'__module__'<\/span>: <span class=\"hljs-string\">'__main__'<\/span>,\n              <span class=\"hljs-string\">'__weakref__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__weakref__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'freedom'<\/span>: <span class=\"hljs-literal\">True<\/span>})<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Put it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> pprint <span class=\"hljs-keyword\">import<\/span> pprint\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Human<\/span><span class=\"hljs-params\">(type)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__new__<\/span><span class=\"hljs-params\">(mcs, name, bases, class_dict)<\/span>:<\/span>\n        class_ = super().__new__(mcs, name, bases, class_dict)\n        class_.freedom = <span class=\"hljs-literal\">True<\/span>\n        <span class=\"hljs-keyword\">return<\/span> class_\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">(object, metaclass=Human)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n\npprint(Person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBwcHJpbnQgaW1wb3J0IHBwcmludAoKCmNsYXNzIEh1bWFuKHR5cGUpOgogICAgZGVmIF9fbmV3X18obWNzLCBuYW1lLCBiYXNlcywgY2xhc3NfZGljdCk6CiAgICAgICAgY2xhc3NfID0gc3VwZXIoKS5fX25ld19fKG1jcywgbmFtZSwgYmFzZXMsIGNsYXNzX2RpY3QpCiAgICAgICAgY2xhc3NfLmZyZWVkb20gPSBUcnVlCiAgICAgICAgcmV0dXJuIGNsYXNzXwoKCmNsYXNzIFBlcnNvbihvYmplY3QsIG1ldGFjbGFzcz1IdW1hbik6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbmFtZSwgYWdlKToKICAgICAgICBzZWxmLm5hbWUgPSBuYW1lCiAgICAgICAgc2VsZi5hZ2UgPSBhZ2UKCgpwcHJpbnQoUGVyc29uLl9fZGljdF9fKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">mappingproxy({<span class=\"hljs-string\">'__dict__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__dict__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'__doc__'<\/span>: None,\n              <span class=\"hljs-string\">'__init__'<\/span>: &lt;<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Person<\/span>.<span class=\"hljs-title\">__init__<\/span> <span class=\"hljs-title\">at<\/span> 0<span class=\"hljs-title\">x10e2db0<\/span>&gt;,\n              '<span class=\"hljs-title\">__module__<\/span>': '<span class=\"hljs-title\">__main__<\/span>',\n              '<span class=\"hljs-title\">__weakref__<\/span>': &lt;<span class=\"hljs-title\">attribute<\/span> '<span class=\"hljs-title\">__weakref__<\/span>' <span class=\"hljs-title\">of<\/span> '<span class=\"hljs-title\">Person<\/span>' <span class=\"hljs-title\">objects<\/span>&gt;,\n              '<span class=\"hljs-title\">freedom<\/span>': <span class=\"hljs-title\">True<\/span>})<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='metaclass-parameters'>Metaclass Parameters <a href=\"#metaclass-parameters\" class=\"anchor\" id=\"metaclass-parameters\" title=\"Anchor for Metaclass Parameters\">#<\/a><\/h2>\n\n\n\n<p>To pass parameters to a metaclass, you use the keyword arguments. For example, the following redefines the <code>Human<\/code> metaclass that accepts keyword arguments, where each argument becomes a class variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Human<\/span><span class=\"hljs-params\">(type)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__new__<\/span><span class=\"hljs-params\">(mcs, name, bases, class_dict, **kwargs)<\/span>:<\/span>\n        class_ = super().__new__(mcs, name, bases, class_dict)\n        <span class=\"hljs-keyword\">if<\/span> kwargs:\n            <span class=\"hljs-keyword\">for<\/span> name, value <span class=\"hljs-keyword\">in<\/span> kwargs.items():\n                setattr(class_, name, value)\n        <span class=\"hljs-keyword\">return<\/span> class_<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following uses the <code>Human<\/code> metaclass to create a <code>Person<\/code> class with the <code>country<\/code> and <code>freedom<\/code> class variables set to <code>USA<\/code> and <code>True<\/code> respectively:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">(object, metaclass=Human, country=<span class=\"hljs-string\">'USA'<\/span>, freedom=True)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here are <code>Person<\/code> class variables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pprint(Person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mappingproxy({<span class=\"hljs-string\">'__dict__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__dict__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'__doc__'<\/span>: <span class=\"hljs-literal\">None<\/span>,\n              <span class=\"hljs-string\">'__init__'<\/span>: &lt;function Person.__init__ at <span class=\"hljs-number\">0x0000018A334235E0<\/span>&gt;,\n              <span class=\"hljs-string\">'__module__'<\/span>: <span class=\"hljs-string\">'__main__'<\/span>,\n              <span class=\"hljs-string\">'__weakref__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__weakref__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">'USA'<\/span>,\n              <span class=\"hljs-string\">'freedom'<\/span>: <span class=\"hljs-literal\">True<\/span>})<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Put it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> pprint <span class=\"hljs-keyword\">import<\/span> pprint\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Human<\/span><span class=\"hljs-params\">(type)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__new__<\/span><span class=\"hljs-params\">(mcs, name, bases, class_dict, **kwargs)<\/span>:<\/span>\n        class_ = super().__new__(mcs, name, bases, class_dict)\n        <span class=\"hljs-keyword\">if<\/span> kwargs:\n            <span class=\"hljs-keyword\">for<\/span> name, value <span class=\"hljs-keyword\">in<\/span> kwargs.items():\n                setattr(class_, name, value)\n        <span class=\"hljs-keyword\">return<\/span> class_\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">(object, metaclass=Human, freedom=True, country=<span class=\"hljs-string\">'USA'<\/span>)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n\npprint(Person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBwcHJpbnQgaW1wb3J0IHBwcmludAoKCmNsYXNzIEh1bWFuKHR5cGUpOgogICAgZGVmIF9fbmV3X18obWNzLCBuYW1lLCBiYXNlcywgY2xhc3NfZGljdCwgKiprd2FyZ3MpOgogICAgICAgIGNsYXNzXyA9IHN1cGVyKCkuX19uZXdfXyhtY3MsIG5hbWUsIGJhc2VzLCBjbGFzc19kaWN0KQogICAgICAgIGlmIGt3YXJnczoKICAgICAgICAgICAgZm9yIG5hbWUsIHZhbHVlIGluIGt3YXJncy5pdGVtcygpOgogICAgICAgICAgICAgICAgc2V0YXR0cihjbGFzc18sIG5hbWUsIHZhbHVlKQogICAgICAgIHJldHVybiBjbGFzc18KCgpjbGFzcyBQZXJzb24ob2JqZWN0LCBtZXRhY2xhc3M9SHVtYW4sIGZyZWVkb209VHJ1ZSwgY291bnRyeT0nVVNBJyk6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbmFtZSwgYWdlKToKICAgICAgICBzZWxmLm5hbWUgPSBuYW1lCiAgICAgICAgc2VsZi5hZ2UgPSBhZ2UKCgpwcHJpbnQoUGVyc29uLl9fZGljdF9fKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='when-to-use-metaclasses'>When to use metaclasses <a href=\"#when-to-use-metaclasses\" class=\"anchor\" id=\"when-to-use-metaclasses\" title=\"Anchor for When to use metaclasses\">#<\/a><\/h2>\n\n\n\n<p>Here&#8217;s the quote from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tim_Peters_(software_engineer)\" target=\"_blank\" rel=\"noreferrer noopener\">Tim Peter<\/a> who wrote the Zen of Python:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Metaclasses are deeper magic that 99% of users should never worry about it. If you wonder whether you need them, you don&#8217;t (the people who actually need them to know with certainty that they need them and don&#8217;t need an explanation about why).<\/p>\n<cite>Tim Peter<\/cite><\/blockquote>\n\n\n\n<p>In practice, you often don&#8217;t need to use metaclasses unless you maintain or develop the core of large frameworks such as Django.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A metaclass is a class that creates other classes.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2964\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass\/\"\n\t\t\t\tdata-post-title=\"Python Metaclass\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2964\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass\/\"\n\t\t\t\tdata-post-title=\"Python Metaclass\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python metaclass and understand how Python uses the metaclasses to create other classes<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":42,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2964","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2964"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2964\/revisions"}],"predecessor-version":[{"id":7182,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2964\/revisions\/7182"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}