<?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>Forem: Shiva Gaire</title>
    <description>The latest articles on Forem by Shiva Gaire (@geeksambhu).</description>
    <link>https://forem.com/geeksambhu</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%2F61785%2Fd0097043-5ec5-47de-a9a5-48eb1850fd21.png</url>
      <title>Forem: Shiva Gaire</title>
      <link>https://forem.com/geeksambhu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/geeksambhu"/>
    <language>en</language>
    <item>
      <title>A Yogi's Guide to Debug Python Programs</title>
      <dc:creator>Shiva Gaire</dc:creator>
      <pubDate>Thu, 14 Sep 2023 18:28:39 +0000</pubDate>
      <link>https://forem.com/geeksambhu/a-yogis-guide-to-debug-python-programs-44h0</link>
      <guid>https://forem.com/geeksambhu/a-yogis-guide-to-debug-python-programs-44h0</guid>
      <description>&lt;p&gt;There are many resources on how to write code but not many on how to debug. In this article, I am highlighting my approach to debug both synchronous and asynchronous Python programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach to debugging
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using IDE:
&lt;/h3&gt;

&lt;p&gt;Running a program in debug mode in an IDE like PyCharm, or Vscode is the easiest way to debug for someone who loves debugging using IDE-assisted features. It provides information regarding the state of objects and available attributes for objects which makes debugging easier. You can set a breakpoint at any given line in your IDE and start debugging and jumping steps repeatedly until the bug is found and fixed.&lt;/p&gt;

&lt;h3&gt;
  
  
  The print statements or &lt;code&gt;logging&lt;/code&gt; module
&lt;/h3&gt;

&lt;p&gt;When I started programming, I mostly used good old print statements and/or the &lt;code&gt;logging&lt;/code&gt; module to know the state of the program in each line. I used to insert random print statements to debug the problems, with funny print or logging statements.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The best debugging tool is still careful thought, coupled with judiciously placed print statements.&lt;br&gt;&lt;br&gt;
- &lt;a href="https://en.wikipedia.org/wiki/Brian_Kernighan"&gt;Brian W. Kernighan&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Rubber Duck
&lt;/h3&gt;

&lt;p&gt;The gist of this debugging approach is to verbally explain the problem that you are facing to someone else, it may be a rubber duck or a colleague(if you are lucky). While explaining the problems to others, our understanding also gets better which will help in connect the dots required for solving problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  REPL
&lt;/h3&gt;

&lt;p&gt;REPL(Read, Evaluate, Print, and Loop), or the way of providing Python statements directly to the interpreter console to evaluate the result. This approach saves time as you can just evaluate the Python statements rather than executing the whole Python file. REPL is mostly useful when dealing with standard modules or just trying to find the results of common data types related functions and the results. REPL to evaluate the results of standard modules/datatypes is a convenient approach to understanding the behavior of underlying operations.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;dir&lt;/code&gt; to look up all attributes available for modules, objects, and data types is still my favorite thing. The REPL below shows the use of &lt;code&gt;dir&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt; module and &lt;code&gt;set&lt;/code&gt; data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import string
&amp;gt;&amp;gt;&amp;gt; dir(string)
['Formatter', 'Template', '_ChainMap', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_sentinel_dict', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
&amp;gt;&amp;gt;&amp;gt; a={1,2,3}
&amp;gt;&amp;gt;&amp;gt; dir(a)
['__and__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
&amp;gt;&amp;gt;&amp;gt; b={3,4,5}
&amp;gt;&amp;gt;&amp;gt; a.intersection(b)
{3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Python debugger or &lt;code&gt;pdb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is something that I have used primarily in my software engineering career to debug Python programs. The module &lt;code&gt;pdb&lt;/code&gt; ( &lt;code&gt;breakpoint&lt;/code&gt; in recent Python versions) temporarily stops the execution of a program and lets you interact with the states of a program. You can insert a breakpoint on any line and move over to the next statement to find and fix problems. Combining &lt;code&gt;pdb&lt;/code&gt; prompt with &lt;code&gt;dir&lt;/code&gt; is a match made in heaven when it comes to debugging. The Python debugger(&lt;code&gt;pdb&lt;/code&gt;) has a set of commands like &lt;code&gt;n&lt;/code&gt;,&lt;code&gt;c&lt;/code&gt;, &lt;code&gt;l&lt;/code&gt; that you can &lt;a href="https://docs.python.org/3/library/pdb.html#debugger-commands"&gt;refer here&lt;/a&gt; to use within the debugging prompt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ python test_requests.py
&amp;gt; /tmp/test_requests.py(6)&amp;lt;module&amp;gt;()
-&amp;gt; print(response.text)
(Pdb) l
  1     import requests
  2
  3     response = requests.get('https://example.org/')
  4
  5     import pdb; pdb.set_trace()
  6  -&amp;gt; print(response.text)
[EOF]
(Pdb) dir(response)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
(Pdb) response.headers
{'Content-Encoding': 'gzip', 'Accept-Ranges': 'bytes', 'Age': '411952', 'Cache-Control': 'max-age=604800', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Sun, 10 Sep 2023 19:57:02 GMT', 'Etag': '"3147526947+gzip"', 'Expires': 'Sun, 17 Sep 2023 19:57:02 GMT', 'Last-Modified': 'Thu, 17 Oct 2019 07:18:26 GMT', 'Server': 'ECS (nyb/1D07)', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'Content-Length': '648'}
(Pdb)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://pypi.org/project/pdbpp/"&gt;&lt;code&gt;pdbpp&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://pypi.org/project/ipdb/"&gt;&lt;code&gt;ipdb&lt;/code&gt;&lt;/a&gt; Python packages available in PyPI to enhances the debugging experience further.&lt;/p&gt;

&lt;h3&gt;
  
  
  Traceback module
&lt;/h3&gt;

&lt;p&gt;I have used the standard &lt;a href="https://docs.python.org/3/library/traceback.html"&gt;&lt;code&gt;traceback&lt;/code&gt;&lt;/a&gt; module to figure out the sequence of functions and their order of execution leading to the exception. It aids in debugging by displaying detailed information on the call stack, line numbers, and source of error. It is useful when the exception is handled without exposing many details of the error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;traceback&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_response&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://thisdoesnot.exist/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
       &lt;span class="n"&gt;traceback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_exc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
       &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Request failed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;inside main&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print_response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  AI-Assisted debugging
&lt;/h3&gt;

&lt;p&gt;LLM tools like Chat GPT, GitHub copilot, Codium AI, etc. are quite good at explaining and even generating code. It can be leveraged while debugging as it can sometimes provide valuable insights into the bug or issue faced.&lt;/p&gt;

&lt;h3&gt;
  
  
  Debugging Asynchronous Python Programs
&lt;/h3&gt;

&lt;p&gt;Debugging synchronous programs is hard, but debugging asynchronous programs is harder.&lt;br&gt;&lt;br&gt;
As mentioned in python documentation, we can enable asyncio debug mode for easier debugging of asynchronous programs.&lt;/p&gt;

&lt;p&gt;Ways to enable asyncio debug mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Setting the &lt;a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONASYNCIODEBUG"&gt;&lt;code&gt;PYTHONASYNCIODEBUG&lt;/code&gt;&lt;/a&gt; environment variable to &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the Python development mode with &lt;code&gt;python -x dev&lt;/code&gt; or by setting the environment variable &lt;a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDEVMODE"&gt;&lt;code&gt;PYTHONDEVMODE&lt;/code&gt;&lt;/a&gt; to 1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Passing &lt;code&gt;debug=True&lt;/code&gt; to &lt;a href="http://asyncio.run"&gt;&lt;code&gt;asyncio.run&lt;/code&gt;&lt;/a&gt;&lt;a href="https://docs.python.org/3/library/asyncio-runner.html#asyncio.run"&gt;&lt;code&gt;()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calling &lt;a href="https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.set_debug"&gt;loop.set_debug()&lt;/a&gt; when the instance of &lt;code&gt;loop&lt;/code&gt; is available.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the benefits of using asyncio debug mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Finds not awaited co-routines and logs them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shows execution time of coroutine or I/O selector&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shows callbacks that take more than 100ms by default. We can also change this time by using &lt;code&gt;loop.slow_callback_duration&lt;/code&gt; to define the minimum seconds to consider slow callbacks&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to the above debug mode, there are tools like &lt;a href="https://aiomonitor.aio-libs.org/en/latest/"&gt;&lt;code&gt;aiomonitor&lt;/code&gt;&lt;/a&gt; , which inspects the asyncio loop and provides debugging capabilities. This exposes the telnet server to provide REPL capabilities, to inspect the state of asyncio application. This can also be used while debugging async programs within a docker container or in a remote server.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Whatever the bug, debugging always requires mental clarity just like a yogi. Don't stress and happy debugging!! 🐍&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.python.org/3/library/asyncio-dev.html#asyncio-debug-mode"&gt;Developing with asyncio - Python documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.python.org/3/library/devmode.html#python-development-mode"&gt;Python Development Mode - Python documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://aiomonitor.aio-libs.org/en/latest/"&gt;aiomonitor’s documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Feel free to comment and If you learned something from this article, please share it with your friends.&lt;/p&gt;

&lt;p&gt;You may also follow me on &lt;a href="https://www.linkedin.com/in/geeksambhu/"&gt;&lt;strong&gt;LinkedIn&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://twitter.com/geeksambhu"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt; .&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>debugging</category>
      <category>asyncio</category>
    </item>
    <item>
      <title>Beginners guide to Iterator, Iteration and Iterable in Python</title>
      <dc:creator>Shiva Gaire</dc:creator>
      <pubDate>Sat, 29 May 2021 12:25:22 +0000</pubDate>
      <link>https://forem.com/geeksambhu/understand-iterator-iteration-and-iterable-in-python-m1j</link>
      <guid>https://forem.com/geeksambhu/understand-iterator-iteration-and-iterable-in-python-m1j</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In programming iterable, iterator and iteration are pretty familiar words and one can have much confusion explaining and understanding these pretty common topics. Let's demystify them one by one. &lt;/p&gt;

&lt;h1&gt;
  
  
  Iterable
&lt;/h1&gt;

&lt;p&gt;The natural language definition of &lt;em&gt;Iterable&lt;/em&gt; is &lt;em&gt;&lt;a href="https://www.thefreedictionary.com/Iterable"&gt;capable of being repeated or iterated&lt;/a&gt;&lt;/em&gt;. &lt;br&gt;
However, the pythonic definition of &lt;strong&gt;iterator&lt;/strong&gt; is an object that implements these two magic methods: &lt;strong&gt;&lt;code&gt;__iter__&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;__getitem__&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;__iter__&lt;/code&gt;&lt;/strong&gt; method of &lt;strong&gt;iterable&lt;/strong&gt; returns an &lt;strong&gt;iterator&lt;/strong&gt;,  which we will see next and similarly the &lt;strong&gt;&lt;code&gt;__getitem__&lt;/code&gt;&lt;/strong&gt; method takes an argument or key item from its sequences starting index zero and until the index is no longer valid and python raises an exception called &lt;a href="https://docs.python.org/3/library/exceptions.html#IndexError"&gt;IndexError&lt;/a&gt;. The &lt;code&gt;__getitem__&lt;/code&gt; method is used mostly for index-based lookup over the collections.&lt;/p&gt;

&lt;p&gt;In python default types: &lt;code&gt;list&lt;/code&gt; &amp;amp; &lt;code&gt;strings&lt;/code&gt; have&lt;code&gt;__iter__&lt;/code&gt; &amp;amp;  &lt;strong&gt;&lt;code&gt;__getitem__&lt;/code&gt;&lt;/strong&gt; implemented and hence we can refer them as an iterable.&lt;/p&gt;

&lt;p&gt;For a more concise visual representation let's assume a single row bookshelf where there are  7 different books. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xAJHFVez--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1622285248628/lnQ7CAlJQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xAJHFVez--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1622285248628/lnQ7CAlJQ.png" alt="image.png" width="207" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On this bookshelf, we can go through each book one by one, also we can change order of the books, can swap the places,  hence we can consider this bookshelf as an &lt;strong&gt;object&lt;/strong&gt;. But as a reader, I can read only one book at a time. &lt;br&gt;
So, it can be clearly seen this bookshelf can be considered iterable from what we have discussed to this point.&lt;/p&gt;
&lt;h1&gt;
  
  
  Iterator
&lt;/h1&gt;

&lt;p&gt;In programming, an iterator is an object that let us travel through the Iterable objects. This iterator object remembers where it is during the process of travelling. &lt;/p&gt;

&lt;p&gt;In python, an iterator is an object with a &lt;code&gt;__next__&lt;/code&gt; method implemented in it.  The &lt;br&gt;
&lt;code&gt;__next__&lt;/code&gt; method returns the next value while traversing(travelling through) the iterable object sequence starting from index zero of the iterable. After each call, the &lt;code&gt;__next__&lt;/code&gt; method updates its state to point to the next object in the iterable sequence. And when the travelling process is completed, it raises the &lt;strong&gt;StopError&lt;/strong&gt; exception.&lt;/p&gt;

&lt;p&gt;For a visual representation, let's assume you have a toy laser light that you can use to point to the book sequentially to read through them one by one, during this process to take out the first book you first point your toy laser light to the first book in the bookshelf, you take out that book and while doing so the laser focus points to next book in the sequence and so on until going through them one by one completes. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I1NyFB2p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1622287169083/nPjKU3cNj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I1NyFB2p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1622287169083/nPjKU3cNj.png" alt="image.png" width="372" height="155"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bookshelf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;The Diary of a young girl&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Crime and punishment&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Romeo and Juliet&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...]&lt;/span&gt; &lt;span class="c1"&gt;#7 books
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;flashlight&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookshelf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flashlight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;Diary&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;young&lt;/span&gt; &lt;span class="n"&gt;girl&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flashlight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Crime&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;punishment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In python, the &lt;code&gt;for&lt;/code&gt; loop, &lt;code&gt;map&lt;/code&gt; and list comprehension automatically call this &lt;code&gt;next&lt;/code&gt; method.&lt;/p&gt;

&lt;h1&gt;
  
  
  Iteration
&lt;/h1&gt;

&lt;p&gt;The &lt;a href="https://www.merriam-webster.com/dictionary/iteration"&gt;Merriam Webster&lt;/a&gt; dictionary defines iteration as: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;the repetition of a sequence of computer instructions a specified number of times or until a condition is met— compare RECURSION&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;one execution of a sequence of operations or instructions in an iteration&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the above bookshelf analogy, we can say the process of repeating through book one by one can be referred to as an iteration. &lt;/p&gt;

&lt;p&gt;In programming, while doing iteration the same block of code is repeated every time. We use loops to achieve iteration that basically executes instruction or sequence of instruction repeatedly.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pythonic Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Iterable&lt;/strong&gt;: list, strings, and any objects that have implemented &lt;code&gt;__getitem__&lt;/code&gt; and &lt;code&gt;__iter__&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Iterator&lt;/strong&gt;: Use of &lt;code&gt;iter()&lt;/code&gt; function on an iterable gives iterator object, Iterator has &lt;strong&gt;next&lt;/strong&gt; method in it. This iterator object remember where it is during the iteration&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Iteration&lt;/strong&gt;: The use of &lt;code&gt;for&lt;/code&gt; loop &lt;code&gt;while&lt;/code&gt; loop or even &lt;code&gt;list comprehension&lt;/code&gt;. &lt;br&gt;
Please leave an example in the comment box :D &lt;/p&gt;

&lt;h1&gt;
  
  
  Reference
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/tutorial/classes.html#iterators"&gt;Iterator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/dev/library/stdtypes.html#iterator-types"&gt;Iterator Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/9884132/what-exactly-are-iterator-iterable-and-iteration"&gt;Iterator, iterators and Iterations&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Finally, If you learned something from this article, please share it with your friends.&lt;/p&gt;

&lt;p&gt;You may also follow me on  &lt;a href="https://www.linkedin.com/in/geeksambhu/"&gt;LinkedIn&lt;/a&gt;  and  &lt;a href="https://twitter.com/geeksambhu"&gt;Twitter&lt;/a&gt; .&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Multiple inheritance and MRO in python</title>
      <dc:creator>Shiva Gaire</dc:creator>
      <pubDate>Mon, 24 May 2021 06:13:29 +0000</pubDate>
      <link>https://forem.com/geeksambhu/multiple-inheritance-and-mro-in-python-20j2</link>
      <guid>https://forem.com/geeksambhu/multiple-inheritance-and-mro-in-python-20j2</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Let's first talk about the word &lt;strong&gt;inheritance&lt;/strong&gt;, oxford dictionary defines it as &lt;em&gt;&lt;a href="https://www.oxfordlearnersdictionaries.com/definition/american_english/inheritance"&gt;something from the past or from your family that affects the way you behave, look, etc.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In object-oriented programming, inheritance facilitates code reuse with the primary motivation of reusing or extending the parent or base class in children or subclass, hence the subclass or children class is affected by the parent class. It however provides freedom to children class to modify the parent's attributes and methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Let's see an example which is most common in all paradigm, here &lt;code&gt;Children&lt;/code&gt; class inherits from &lt;code&gt;Parent&lt;/code&gt; class and inherits some methods of &lt;code&gt;Parent&lt;/code&gt; class and also overrides the &lt;code&gt;Parent&lt;/code&gt; class implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parent_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parent_function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;common_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Parent&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s common_function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Children&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;child_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Child&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s child_function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;common_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Child&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s common_function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Children&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parent_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# #prints 'parent_function'
&lt;/span&gt;
    &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;child_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;#prints 'child's function'
&lt;/span&gt;    &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parent_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;#prints 'parent_function'
&lt;/span&gt;
    &lt;span class="c1"&gt;# common of both
&lt;/span&gt;    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;common_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;#prints "Parent's common_function"
&lt;/span&gt;    &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;common_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;#prints "Child's common_function"
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type of single inheritance is common across all Object-Oriented Paradigm, However, Python has something more to offer with multiple inheritance, which means you can inherit from multiple base classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple Inheritance
&lt;/h2&gt;

&lt;p&gt;Multiple Inheritance refers to the inheritance that uses two or more base class. This is how you define multiple inheritance using &lt;code&gt;class&lt;/code&gt; keyword for multiple &lt;code&gt;baseClass&lt;/code&gt;:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;class SubClass(baseClass, baseClass2, baseClass3, ....)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Python has its renowned multiple inheritance feature which comes in handy when working with a class that needs implementation from two or more class.&lt;/p&gt;

&lt;p&gt;When working with multiple base class there arises confusion if one or more base class implements the same method. Let's see how this is handled in python in next step: &lt;/p&gt;

&lt;h2&gt;
  
  
  Method Resolution Order
&lt;/h2&gt;

&lt;p&gt;Multiple inheritance can create confusion when there are one or more base classes that implement the same method. Python(2.3 and newer) version introduced the renowned &lt;code&gt;method resolution order&lt;/code&gt; or &lt;code&gt;mro&lt;/code&gt; to deal with the method resolution while using multiple inheritance. &lt;/p&gt;

&lt;p&gt;Method resolution order(MRO) in short has its root in  &lt;a href="https://en.wikipedia.org/wiki/C3_linearization"&gt;C3 linearization algorithm&lt;/a&gt; which basically outlines the basis for MRO list calculation in python multilevel inheritance. Method Resolution Order is the python provided calculation of inheritance graph. &lt;/p&gt;

&lt;p&gt;The MRO calculation has 3 major points to consider for the method resolution list from a subclass. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Subclasses comes first rather than the base classes&lt;/li&gt;
&lt;li&gt;Base class definition order is preserved&lt;/li&gt;
&lt;li&gt;Fits the above two criteria for all MRO calculation in a program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this is violated then C3 prohibits the python from inheritance declaration. &lt;/p&gt;

&lt;p&gt;Let's see an example to see the &lt;code&gt;mro&lt;/code&gt; list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi from A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi From B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi from C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;E&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;


&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__mro__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;# (&amp;lt;class '__main__.D'&amp;gt;, &amp;lt;class '__main__.B'&amp;gt;, &amp;lt;class '__main__.C'&amp;gt;, &amp;lt;class '__main__.A'&amp;gt;, &amp;lt;class 'object'&amp;gt;)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__mro__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;# ((&amp;lt;class '__main__.E'&amp;gt;, &amp;lt;class '__main__.C'&amp;gt;, &amp;lt;class '__main__.B'&amp;gt;, &amp;lt;class '__main__.A'&amp;gt;, &amp;lt;class 'object'&amp;gt;))
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the &lt;code&gt;__mro__&lt;/code&gt; attribute often pronounced as &lt;code&gt;dun~der mro&lt;/code&gt; (double underscore mro) with class name, It gives the tuple of classes defining the method's resolution order for the subclass.&lt;/p&gt;

&lt;p&gt;In the above example, we can see the diamond inheritance formation. So, on printing  &lt;code&gt;D.__mro__&lt;/code&gt; attribute,  it gives:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;(&amp;lt;class '__main__.D'&amp;gt;, &amp;lt;class '__main__.B'&amp;gt;, &amp;lt;class '__main__.C'&amp;gt;, &amp;lt;class '__main__.A'&amp;gt;, &amp;lt;class 'object'&amp;gt;)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and on printing &lt;code&gt;E.__mro__&lt;/code&gt; , it gives: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;(&amp;lt;class '__main__.E'&amp;gt;, &amp;lt;class '__main__.C'&amp;gt;, &amp;lt;class '__main__.B'&amp;gt;, &amp;lt;class '__main__.A'&amp;gt;, &amp;lt;class 'object'&amp;gt;)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Notice the difference in the second element of the above tuple, its because the base class order is preserved for the class definition of class D and E:  &lt;strong&gt;&lt;code&gt;D(B, C)&lt;/code&gt; and &lt;code&gt;E(C, B)&lt;/code&gt;&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;So, &lt;code&gt;&amp;lt;class '__main__.C'&amp;gt;&lt;/code&gt; in the second index for the &lt;code&gt;E&lt;/code&gt; subclass and &lt;code&gt;&amp;lt;class '__main__.B'&amp;gt;&lt;/code&gt; in the second index for &lt;code&gt;B&lt;/code&gt; subclass.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The method resolution order will be entirely based on the above tuple order,&lt;br&gt;&lt;br&gt;
which means when any method is invoked for the instance of class &lt;code&gt;D&lt;/code&gt;, the method of class &lt;code&gt;D&lt;/code&gt; will be resolved first and if not found it will try to resolve from the second element from the &lt;code&gt;D.__mro__&lt;/code&gt; tuple , which is class &lt;code&gt;B&lt;/code&gt; and so on to the next element, if no such method is implemented it will result in &lt;code&gt;AttributeError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the implementation is found for the current lookup the search stops right there and the method will be resolved from there. &lt;/p&gt;

&lt;p&gt;If you made it upto here, Please come up in discussion to check the &lt;code&gt;__mro__&lt;/code&gt; for the below snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi from A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi From B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi from C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Finally, If you learned something from this article, please share it with your friends.&lt;/p&gt;

&lt;p&gt;You may also follow me on  &lt;a href="https://www.linkedin.com/in/geeksambhu/"&gt;LinkedIn&lt;/a&gt;  and  &lt;a href="https://twitter.com/geeksambhu"&gt;Twitter&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>python3</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Big O notation and Algorithmic Complexity analysis</title>
      <dc:creator>Shiva Gaire</dc:creator>
      <pubDate>Tue, 20 Nov 2018 04:21:52 +0000</pubDate>
      <link>https://forem.com/geeksambhu/big-o-notation-and-algorithmic-complexity-analysis--71c</link>
      <guid>https://forem.com/geeksambhu/big-o-notation-and-algorithmic-complexity-analysis--71c</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;When executing a particular task using algorithms, one common thing that hits every problem solver brain is the efficient and fast algorithm to solve that problem. But, what do exactly fast and efficient means? &lt;/p&gt;

&lt;p&gt;Is it the measure of real processing time? &lt;/p&gt;

&lt;p&gt;No, it's not the measure of real-time like second and minutes because the old computer with Pentium processor may take a long time to process the same algorithm than the new Intel i3 processor, or, A bad algorithm written in Assembly may execute faster than a good algorithm written in &lt;code&gt;python&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;So, run time of program can't be considered to measure algorithmic efficiency. &lt;/p&gt;

&lt;p&gt;Hence, to measure the algorithmic efficiency, the concept of &lt;strong&gt;Asymptotic Complexity&lt;/strong&gt; of a program and a notation for describing this called &lt;strong&gt;Big O&lt;/strong&gt;  was introduced. &lt;strong&gt;Big O&lt;/strong&gt; is the worst case, &lt;strong&gt;Big Omega&lt;/strong&gt; and &lt;strong&gt;Big Theta&lt;/strong&gt; are best and average case notations. Worst case means we are mostly unlucky for that problem domain , i.e precondition of task do not favour us. Complexity analysis is a tool that allows us to explain how an algorithm behaves as the input grows larger.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenarios
&lt;/h3&gt;

&lt;p&gt;Sorting an array of size &lt;code&gt;10000&lt;/code&gt; compared to array of size &lt;code&gt;100&lt;/code&gt; and analyzing how run time of the program grows.&lt;/p&gt;

&lt;p&gt;Now, Lets dive deeper,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Counting the number of character in a string:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt; One simplest approach is traversing through whole string letter by letter and incrementing a counter variable by 1. This algorithmic approach run in linear time with respect to number of character '&lt;strong&gt;n&lt;/strong&gt;' in the string, i.e, it runs in &lt;strong&gt;O(n). &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why ??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using this approach the time required to traverse the entire string is proportional to number of character in string, &lt;br&gt;
i.e time required to traverse string with 40 character is twice the time required to trverse string with 20 character as the amount of time to look individual character is same.&lt;/p&gt;

&lt;p&gt;Another approach is, declaring a variable and storing the number of character in a variable say "length" early in the program, i.e, before storing the very first character. Now, there is no need to look at string, instead one have to check the value of that variable. The accessing of such variable is generally asymptotically constant time operation, or &lt;strong&gt;O(1).&lt;/strong&gt; This is because Asymptotic means "How the run time change as input grows". In this approach the length of string whether it has one character or thousandsof character, the only thing we need to do is to find string length and which can be done by reading the "length" variable and the reading time for this variable is constant regardless of string size. Hence this approach can be referred as running in constant time.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;O(1)&lt;/strong&gt; does not change with the size of inputs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variations
&lt;/h3&gt;

&lt;p&gt;There are many different Big O runtimes like &lt;strong&gt;O(n), O(n&lt;/strong&gt;²) etc&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O(n&lt;/strong&gt;²) are asymptotically slower than &lt;strong&gt;O(n)&lt;/strong&gt; i.e if &lt;strong&gt;n&lt;/strong&gt; grows  &lt;strong&gt;O(n&lt;/strong&gt;²) will take more time than &lt;strong&gt;O(n). &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This dosen't means &lt;strong&gt;O(n)&lt;/strong&gt; always run faster, maybe if input is smaller then  &lt;strong&gt;O(n&lt;/strong&gt;²) may work faster yet unnoticed&lt;/p&gt;

&lt;p&gt;Similarly, we may have logarithmic &lt;strong&gt;O(log(n))&lt;/strong&gt; for some cases like in Binary search. Binary search cut the array size in half with each operation.&lt;/p&gt;

&lt;p&gt;Simpler Program can be analysed by counting number of nested loop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A single loop over n items has &lt;strong&gt;O(n)&lt;/strong&gt; complexity.&lt;/li&gt;
&lt;li&gt;  A loop within a loop has &lt;strong&gt;O(n&lt;/strong&gt;²) complexity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;| &lt;a href="https://1.bp.blogspot.com/-q_If4WoaJTA/WQz5mDhm2QI/AAAAAAAATGg/AWr6IyaHI98WN5aZL0XY5hH4Rmn2XmghACLcB/s1600/Screenshot%2Bfrom%2B2017-05-06%2B04-00-15.png" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-q_If4WoaJTA%2FWQz5mDhm2QI%2FAAAAAAAATGg%2FAWr6IyaHI98WN5aZL0XY5hH4Rmn2XmghACLcB%2Fs320%2FScreenshot%252Bfrom%252B2017-05-06%252B04-00-15.png" title="Big Oh notation" alt="Different variations of Big O" width="320" height="272"&gt;&lt;/a&gt; |&lt;br&gt;
| Big O Variant |&lt;/p&gt;

&lt;p&gt;The Original Blog post about this is &lt;a href="https://www.shivagaire.com.np/2017/05/05/big-oh-notation-and-algorithmic.html" rel="noopener noreferrer"&gt;here.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>timecomplexity</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
