<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Clouditate</title>
    <description>The latest articles on DEV Community by Clouditate (@clouditate).</description>
    <link>https://dev.to/clouditate</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F371289%2Ff8f54b4f-2bcf-465a-b898-0ddec18cf74a.png</url>
      <title>DEV Community: Clouditate</title>
      <link>https://dev.to/clouditate</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/clouditate"/>
    <language>en</language>
    <item>
      <title>Python meta classes</title>
      <dc:creator>Clouditate</dc:creator>
      <pubDate>Tue, 21 Apr 2020 20:59:52 +0000</pubDate>
      <link>https://dev.to/clouditate/python-meta-classes-59mh</link>
      <guid>https://dev.to/clouditate/python-meta-classes-59mh</guid>
      <description>&lt;h2&gt;What is a python metaclass&lt;/h2&gt;

&lt;p&gt;Metaclasses in python are a useful way to describe the behavior of a class. &lt;strong&gt;Metaprogramming&lt;/strong&gt; is a programming technique used in a computer software component to have the ability to treat other components as described in their data. It means that a program can be designed to understand and even change other programs or modify it's specification while executing.  Python has a perfect syntax structure and modules that support meta class definition. Many people are better off not using metaclasses in python, but if you're using the Django web framework, metaclasses are used in model and form definition. Read more on &lt;a href="https://clouditate.com/what-is-a-meta-class-in-django/"&gt;Django metaclasses&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;A meta class in python can simply be defined as a class of &lt;strong&gt;class&lt;/strong&gt; that describes the behavior of the &lt;strong&gt;class&lt;/strong&gt;. A class is the instance of a meta class&lt;/p&gt;

&lt;p&gt;&lt;a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Lisp_(programming_language)"&gt;Lisp programming language&lt;/a&gt; has a reputation for having a good meta programming facility and easy syntax. I advice you check basic Lisp meta programming.&lt;/p&gt;

&lt;p&gt;Python has old style and new style class definition syntax. Let's explore of those class varieties:&lt;/p&gt;

&lt;h3&gt;Old style python class definition&lt;/h3&gt;

&lt;p&gt;Classes are not defined by inheriting from a base class. An instance of an old-style class is implemented from an instance.  In old classes, obj.__class__ represent the class and type(obj) return the instance of the class. Here is an example:&lt;/p&gt;

&lt;pre class="wp-block-syntaxhighlighter-code"&gt;#
#
# Old style class definition

class Hello:
   pass

obj = Hello()
obj.__class__

type(obj)

#
#
#&lt;/pre&gt;



&lt;p&gt;The old style python class definition was removed in python 3 &amp;gt;. Let's look at the new style of defining classes.&lt;/p&gt;



&lt;h3&gt;New style python class definition&lt;/h3&gt;



&lt;p&gt;A class is created by inheriting from Object class. It is the only way to define a class since &lt;strong&gt;python 3&lt;/strong&gt;. In the new style definition obj.__class__ is the same as type(obj). That's the main difference between the two python class definition. Example code:&lt;/p&gt;



&lt;pre class="wp-block-syntaxhighlighter-code"&gt;#
#
# New class definition example

class MyClass(Object):
   pass

obj = MyClass()
print(type(obj) is obj.__class__)

# end example
#
#&lt;/pre&gt;



&lt;p&gt;The above code will return true because obj.__class__ is same as type(obj). &lt;/p&gt;







&lt;h2&gt;Python classes and types&lt;/h2&gt;



&lt;p&gt;Everything in python&lt;strong&gt; is an object. So are classes. Let's look at the type of class. The type of an object is a class, but the type of&lt;/strong&gt; class is &lt;strong&gt;type&lt;/strong&gt;. &lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Type&lt;/strong&gt; is the metaclass and classes are the instances of the metaclass. This means that any python 3 class is an instance of the meta class.&lt;/p&gt;







&lt;h2&gt;Creating custom python metaclass&lt;/h2&gt;



&lt;p&gt;Python allows a programmer to customize the class creation by passing the metaclass keyword in the class definition syntax.  You can also do so by inheriting a class in which a metaclass was defined. Here is an example.&lt;/p&gt;



&lt;pre class="wp-block-syntaxhighlighter-code"&gt;#
#
# Define Meta Class
class MyMetaClass(type):
    pass

# Define a class that takes the MyMetaClass 
class MyClass(metaclass=MyMetaClass):
    pass

# A class thatinherits a MyClass class
class MyChildclass(MyClass):
    pass

#
#
#
&lt;/pre&gt;

&lt;p&gt;If you define a class and decide not to define a custom meta class, the default &lt;strong&gt;type&lt;/strong&gt; is used.&lt;/p&gt;

&lt;h2&gt;__new__ and __init__ , python meta classes&lt;/h2&gt;

&lt;p&gt;You can define python metaclasses using&lt;strong&gt; __new__ and __init__&lt;/strong&gt; keywords. Let's look at the example implimentation.&lt;/p&gt;

&lt;pre class="wp-block-syntaxhighlighter-code"&gt;#
#
#

class MetaClass(type):
    def __new__(cls, name, bases, dict):
        pass

class AnotherMetaClass(type):
    def __init__(self, name, bases, dict):
        pass

#
#
#&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;__new__&lt;/strong&gt; is used if you want to define a dictionary (dict) or tuples before you create the class. &lt;strong&gt;__new__ &lt;/strong&gt;is called after the object has been created to initialize it.&lt;/p&gt;



&lt;h2&gt;Singleton Class using a Metaclass&lt;/h2&gt;

&lt;p&gt;Singleton class is a class that can be instantiated by only one object. In python you can implement a singleton design using metaclasses. Example code.&lt;/p&gt;

&lt;pre class="wp-block-syntaxhighlighter-code"&gt;#
#
#
class SingletonMetaClass(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(SingletonMeta,cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class MyClass(metaclass=SingletonMeta):
    pass
#
#
#&lt;/pre&gt;

</description>
    </item>
  </channel>
</rss>
