{"id":214,"date":"2024-01-25T16:10:50","date_gmt":"2024-01-25T16:10:50","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=214"},"modified":"2024-01-25T16:10:53","modified_gmt":"2024-01-25T16:10:53","slug":"python-type-class","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-type-class\/","title":{"rendered":"Python type Class"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python type class and understand how Python uses the type class to create other classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python type class<\/h2>\n\n\n\n<p>In Python, a\u00a0class\u00a0is an object of the class\u00a0<code>type<\/code>. For example, the following defines the\u00a0<code>Person<\/code>\u00a0class with two\u00a0methods\u00a0<code>__init__<\/code>\u00a0and\u00a0<code>greeting<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name, age): self.name = name self.age = age def greeting(self): return f'Hi, I am {self.name}. I am {self.age} year old.'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person<\/code>\u00a0class is an object of the class\u00a0<code>type<\/code>\u00a0as shown in the following statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(type(Person))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;class 'type'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person<\/code>\u00a0class is an instance of the\u00a0<code>type<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(isinstance(Person, type))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Python uses the type class to create other classes. The type class itself is a callable. The following shows one of the constructors of the type class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>type(name, bases, dict) -> a new type<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The constructor has three parameters for creating a new class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>name<\/code>: is the name of the class e.g., Person<\/li>\n\n\n\n<li><code>bases<\/code>&nbsp;is a tuple that contains the base classes of the new class. For example, the Person inherits from the object class, so the bases contains one class (object,)<\/li>\n\n\n\n<li><code>dict<\/code>&nbsp;is the class namespace<\/li>\n<\/ul>\n\n\n\n<p>Technically, you can use the&nbsp;<code>type<\/code>&nbsp;class to create a class dynamically. Before doing it, you need to understand how Python creates the classes.<\/p>\n\n\n\n<p>When the Python interpreter encounters a class definition in the code, it will:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, extract the class body as string.<\/li>\n\n\n\n<li>Second, create a class dictionary for the class namespace.<\/li>\n\n\n\n<li>Third, execute the class body to fill up the class dictionary.<\/li>\n\n\n\n<li>Finally, create a new instance of type using the above&nbsp;<code>type()<\/code>&nbsp;constructor.<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s emulate the steps above to create a&nbsp;<code>Person<\/code>&nbsp;class dynamically:<\/p>\n\n\n\n<p>First, extract the class body:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class_body = \"\"\" def __init__(self, name, age): self.name = name self.age = age def greeting(self): return f'Hi, I am {self.name}. I am {self.age} year old.' \"\"\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, create a class dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class_dict = {}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Third, execute the class body and fill up the class dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>exec(class_body, globals(), class_dict)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>exec()<\/code>&nbsp;function executes the class body and fills up the global and class namespaces.<\/p>\n\n\n\n<p>Finally, create a new\u00a0<code>Person<\/code>\u00a0class using the\u00a0<code>type<\/code>\u00a0constructor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Person = type('Person', (object,), class_dict)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Note that the&nbsp;<code>Person<\/code>&nbsp;is a class, which is also an object. The&nbsp;<code>Person<\/code>&nbsp;class inherits from the&nbsp;<code>object<\/code>&nbsp;class and has the namespace of the&nbsp;<code>class_dict<\/code>.<\/p>\n\n\n\n<p>The following shows the type of the\u00a0<code>Person<\/code>\u00a0class which is the\u00a0<code>type<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;class 'type'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And it is an instance of the\u00a0<code>type<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(isinstance(Person, type))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The class_dict has the two functions\u00a0<code>__init__<\/code>\u00a0and\u00a0<code>greeting<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'__init__': &lt;function __init__ at 0x0000024E28465160>, 'greeting': &lt;function greeting at 0x0000024E28931550>}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person.__dict__<\/code>\u00a0also contains these functions :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>mappingproxy({'__dict__': &lt;attribute '__dict__' of 'Person' objects>, '__doc__': None, '__init__': &lt;function __init__ at 0x00000165F7725160>, '__module__': '__main__', '__weakref__': &lt;attribute '__weakref__' of 'Person' objects>, 'greeting': &lt;function greeting at 0x00000165F7F71550>})<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, Python creates the&nbsp;<code>type<\/code>&nbsp;class dynamically, which is the same as the one that you define statically in the code.<\/p>\n\n\n\n<p>Because the&nbsp;<code>type<\/code>&nbsp;class creates other classes, we often refer to it as a&nbsp;<strong>metaclass<\/strong>. A metaclass is a class used to create other classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python type class and understand how Python uses the type class to create other classes. Introduction to the Python type class In Python, a\u00a0class\u00a0is an object of the class\u00a0type. For example, the following defines the\u00a0Person\u00a0class with two\u00a0methods\u00a0__init__\u00a0and\u00a0greeting: The\u00a0Person\u00a0class is an object of the class\u00a0type\u00a0as shown in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-8-metaprogramming-exceptions"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/214\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}