<?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: Arjun</title>
    <description>The latest articles on DEV Community by Arjun (@arjunsna).</description>
    <link>https://dev.to/arjunsna</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%2F260464%2Fb69f2671-a042-4496-8c27-6caf83815599.jpg</url>
      <title>DEV Community: Arjun</title>
      <link>https://dev.to/arjunsna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arjunsna"/>
    <language>en</language>
    <item>
      <title>Python Metaclass(type)</title>
      <dc:creator>Arjun</dc:creator>
      <pubDate>Fri, 20 Mar 2020 13:14:08 +0000</pubDate>
      <link>https://dev.to/arjunsna/python-metaclass-type-4o94</link>
      <guid>https://dev.to/arjunsna/python-metaclass-type-4o94</guid>
      <description>&lt;p&gt;Metaclasses in python are a way of supporting metaprogramming. Python has built-in metaclasses and also supports creating custom metaclasses. Understanding metaclasses in python helps to understand under the hood of classes and objects in python. &lt;/p&gt;

&lt;p&gt;In python, to get the instance of or type of or class of an object, there are two ways → accessing &lt;code&gt;__class__&lt;/code&gt;attribute of the object and calling &lt;code&gt;type(obj)&lt;/code&gt;function. Both return the same value in new-style classes. Old-style classes before 3 have a different result for those though&lt;/p&gt;

&lt;p&gt;So in python, we can refer to the object's type and it's class interchangeably. Everything in python is an object.  If so, then a class should also be an object. Then what is the type if the class object or what is the parent class of class object? We will use &lt;code&gt;type()&lt;/code&gt; to find the type of or class of the class object&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Foo:
    pass

x = Foo()

type(x) // &amp;lt;class '__main__.Foo'&amp;gt;
type(Foo) // &amp;lt;class 'type'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As shown in the code above &lt;code&gt;type(x)&lt;/code&gt; will print &lt;code&gt;&amp;lt;class '__main__.Foo'&amp;gt;&lt;/code&gt;because &lt;code&gt;x&lt;/code&gt; is of type &lt;code&gt;Foo&lt;/code&gt; or x is an object of class &lt;code&gt;Foo&lt;/code&gt;. But the &lt;code&gt;type(Foo)&lt;/code&gt; prints  &lt;code&gt;&amp;lt;class 'type'&amp;gt;&lt;/code&gt; which say the &lt;code&gt;Foo&lt;/code&gt; is of type &lt;code&gt;type&lt;/code&gt;.  &lt;code&gt;Foo&lt;/code&gt; is an object of &lt;code&gt;type&lt;/code&gt; class. Not only the user-defined classes, but all the built-in classes will also have the same result which is &lt;code&gt;&amp;lt;class 'type'&amp;gt;&lt;/code&gt;. And again as mentioned, all classes are instances/objects of &lt;code&gt;type&lt;/code&gt; class, then &lt;code&gt;type&lt;/code&gt; class should also be an object of a class which is &lt;code&gt;type&lt;/code&gt; itself. In the short, the class of or parent class of &lt;code&gt;type&lt;/code&gt;class is &lt;code&gt;type&lt;/code&gt; itself.  And this &lt;code&gt;type&lt;/code&gt; is called a metaclass. How classes are created with metaclass type? Using the &lt;code&gt;type&lt;/code&gt; function. The &lt;code&gt;type&lt;/code&gt; function when passed a single argument, returns the type of the argument. The same function can also be used to create a class dynamically as below.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Foo:
    pass

Bar = type('Bar', (Foo,), dict(attr=100)) 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code creates a new class named &lt;code&gt;Bar&lt;/code&gt;, inheriting from base class &lt;code&gt;Foo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let see what those argument are. The syntax is &lt;code&gt;type(&amp;lt;name&amp;gt;, &amp;lt;base classes&amp;gt;, &amp;lt;attibutes&amp;gt;)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;name&amp;gt;&lt;/code&gt;: the name of the class to be created.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;base classes&amp;gt;&lt;/code&gt;: a tuple object of all classed to inherit from.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;attibutes&amp;gt;&lt;/code&gt;: a dictionary object of all the attributes to be added to the created class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instance(class and object of class) creation flow
&lt;/h2&gt;

&lt;p&gt;Knowing the flow of instance creation helps understand metaclasses better.&lt;/p&gt;

&lt;p&gt;Creating a class instance which is basically creating objects from classes can be depicted from the below diagram&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s2l1rxR3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3g8zuwq66fzr9ykn3hb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s2l1rxR3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3g8zuwq66fzr9ykn3hb2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The diagram is more self-explanatory. The flow is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a new object for a class is created, the &lt;code&gt;__call__&lt;/code&gt; method of the metaclass is called. In this case, the metaclass will be &lt;code&gt;type&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The call method calls the &lt;code&gt;__new__&lt;/code&gt; method of the class. This is the method which creates an instance and returns it to the &lt;code&gt;__call__&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Then the &lt;code&gt;__init__&lt;/code&gt; method of the class is called with the created instance as the first parameter.&lt;/li&gt;
&lt;li&gt;Then the instance is returned  from the &lt;code&gt;__call__&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not let us look at the creation of an instance of a metaclass which is creating a class itself.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--spiBnzj7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2oxyxubtifk7q4ngnjvq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--spiBnzj7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2oxyxubtifk7q4ngnjvq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Let's see the flow &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initially, &lt;code&gt;__prepare__&lt;/code&gt; method is called which can return a dictionary that will be used as a local namespace in the created class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Meta-metaclass&lt;/code&gt;, which is in normal cases the &lt;code&gt;type&lt;/code&gt; class &lt;code&gt;__call__&lt;/code&gt; method is invoked, which calls &lt;code&gt;__new__&lt;/code&gt; method of the metaclass. This is where the class object is created and returned.&lt;/li&gt;
&lt;li&gt;Then the &lt;code&gt;__init__&lt;/code&gt; method is invoked with created class a first parameter&lt;/li&gt;
&lt;li&gt;Once &lt;code&gt;__init__&lt;/code&gt; returns, the created class is returned&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>metaclass</category>
    </item>
    <item>
      <title>Character set and Character encoding</title>
      <dc:creator>Arjun</dc:creator>
      <pubDate>Sun, 15 Mar 2020 18:12:55 +0000</pubDate>
      <link>https://dev.to/arjunsna/character-set-and-character-encoding-5479</link>
      <guid>https://dev.to/arjunsna/character-set-and-character-encoding-5479</guid>
      <description>&lt;p&gt;Machines understand 0s and 1s. Character sets are created to convert characters between machine code and human-readable symbols. For example, the character &lt;strong&gt;&lt;code&gt;a&lt;/code&gt;&lt;/strong&gt; will be encoded based on the character set when it is stored in memory. And when the character is displayed, a decoder is used to convert the content in the memory into a human-readable symbol.&lt;/p&gt;

&lt;p&gt;ASCII is one of the character set which represents a character in 7bit. Since it uses 7 bits, there can be 2^7, 128 characters represented in ASCII. ASCII contains only English alphabets&lt;/p&gt;

&lt;p&gt;So with ASCII, character &lt;code&gt;a&lt;/code&gt; will be stored in its binary representation of  &lt;code&gt;01100001&lt;/code&gt; which is 97 in decimal which is the character code of &lt;code&gt;a&lt;/code&gt; in ASCII.&lt;/p&gt;

&lt;p&gt;Unicode was created to have a universal code for almost all characters across all languages in the world and some commonly used symbols. Unicode uses 1 to 6 bytes. To encode and decode Unicode characters, different techniques were used like UTF-8, UTF-16, UTF-32, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In UTF-8 each character is encoded into 1 to 4 bytes ( the dominant encoding )&lt;/li&gt;
&lt;li&gt;In UTF16 each character is encoded into 1 to two 16-bit words and&lt;/li&gt;
&lt;li&gt;in UTF-32 every character is encoded as a single 32-bit word.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>characterset</category>
      <category>encoding</category>
      <category>ascii</category>
      <category>unicode</category>
    </item>
  </channel>
</rss>
