<?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: P0intMaN</title>
    <description>The latest articles on Forem by P0intMaN (@p0intman).</description>
    <link>https://forem.com/p0intman</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%2F728260%2Fc327b8dd-5d6d-435a-98dd-8151cc63ae83.jpeg</url>
      <title>Forem: P0intMaN</title>
      <link>https://forem.com/p0intman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/p0intman"/>
    <language>en</language>
    <item>
      <title>How I cracked MLH Fellowship in my first attempt: Journey &amp; Tips</title>
      <dc:creator>P0intMaN</dc:creator>
      <pubDate>Tue, 02 Nov 2021 11:22:49 +0000</pubDate>
      <link>https://forem.com/p0intman/how-i-cracked-mlh-fellowship-in-my-first-attempt-journey-tips-127c</link>
      <guid>https://forem.com/p0intman/how-i-cracked-mlh-fellowship-in-my-first-attempt-journey-tips-127c</guid>
      <description>&lt;p&gt;I am pretty much sure you know what &lt;strong&gt;MLH Fellowship&lt;/strong&gt; is. For those who don't know about this, let me brief you about this amazing program. &lt;strong&gt;MLH Fellowship&lt;/strong&gt;, powered by &lt;strong&gt;GitHub&lt;/strong&gt;, &lt;em&gt;is a&lt;/em&gt; &lt;em&gt;global program aimed at providing experience to talented individuals with an unlimited thirst for learning&lt;/em&gt;. You would be learning the in-demand technologies from the leading tech mentors. Also, you will be put in a healthy competitive environment consisting of other &lt;strong&gt;hand-picked fellows&lt;/strong&gt;, with similar interests and zeal.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ejxak5u1yug6y4h23p1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ejxak5u1yug6y4h23p1.png" alt="Accepted"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My Journey
&lt;/h2&gt;

&lt;p&gt;I applied for MLH Fellowship in the &lt;strong&gt;early weeks of October&lt;/strong&gt; from the official &lt;strong&gt;&lt;a href="https://fellowship.mlh.io/" rel="noopener noreferrer"&gt;MLH website&lt;/a&gt;&lt;/strong&gt;. Then, I got scheduled with my &lt;strong&gt;Initial Interview&lt;/strong&gt; (kind of behavioral). After a week, I gave my &lt;strong&gt;Technical Interview&lt;/strong&gt; and finally, after another week's wait, I received the result and guess what, &lt;strong&gt;&lt;em&gt;I got selected.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was the very first interview I have ever given and the result is quite motivating! If I can do this, so could you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Selection Process
&lt;/h2&gt;

&lt;p&gt;The entire procedure is neatly divided into three phases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Application&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Interviews&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Final Review&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand each process in detail&lt;/p&gt;

&lt;h3&gt;
  
  
  The Application
&lt;/h3&gt;

&lt;p&gt;In this phase, you would be filling out an application form. The form contains &lt;strong&gt;choice-based questions&lt;/strong&gt; (like, &lt;em&gt;Which programming language(s) are you comfortable working with?&lt;/em&gt; And then they provide some choices), &lt;strong&gt;essay questions&lt;/strong&gt; (like, &lt;em&gt;Why are you applying for MLH Fellowship?&lt;/em&gt;) and finally, &lt;strong&gt;detail-based questions&lt;/strong&gt; (like, &lt;em&gt;Your Name, GitHub profile link, Code Sample&lt;/em&gt; etc).&lt;/p&gt;

&lt;p&gt;The form filling is a fairly easy process and it would take around &lt;strong&gt;&lt;em&gt;10--13 mins&lt;/em&gt;&lt;/strong&gt; to fill out the form. Once you have submitted the form, you'd have to wait for a considerable period of time (probably, for around 10--15 days) before getting to know your result. During this period, your application is reviewed by multiple MLH recruiters. This is done to filter out the &lt;strong&gt;most apt and genuine application from a vast pool of applications&lt;/strong&gt;. If your application is unique or interesting, then you'd be called in for the interview phase.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Interviews
&lt;/h3&gt;

&lt;p&gt;There would be two interviews: &lt;strong&gt;&lt;em&gt;Initial and Technical&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Initial interview's purpose is to find out whether you are an &lt;strong&gt;appropriate candidate who can successfully complete the MLH Fellowship&lt;/strong&gt;. The questions asked in this round are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why do you think you are a good fit for MLH Fellowship?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;How do you keep yourself updated with the new trends in technology?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;You have mentioned &lt;em&gt;this&lt;/em&gt; in your application form. Could you please tell me about a situation where you demonstrated &lt;em&gt;this&lt;/em&gt;?&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The duration of this interview was roughly around &lt;strong&gt;15-25 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Technical interview is primarily based on the &lt;strong&gt;&lt;em&gt;code sample&lt;/em&gt;&lt;/strong&gt; that you have submitted in the application form. You would be given &lt;strong&gt;most of the control&lt;/strong&gt; where you'd have to do a &lt;strong&gt;brief code walkthrough&lt;/strong&gt;. The interviewer will then stop you in between and ask questions related to certain concepts. In my interview, since I demonstrated a &lt;strong&gt;&lt;em&gt;Flask application&lt;/em&gt;&lt;/strong&gt;, I was asked the following major questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Why did you prefer Flask? You could have also opted for other frameworks (like DJango)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What is a decorator? Explain to me briefly&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What is an ORM? How is it useful to a developer who wants to create rapid applications?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Several other concept based questions.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing to note here is that, you need to have an &lt;strong&gt;in-depth knowledge&lt;/strong&gt; of what you are demonstrating. The entire duration of this interview was around &lt;strong&gt;20-30 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Final Review
&lt;/h3&gt;

&lt;p&gt;After successfully completing both the interviews, your &lt;strong&gt;entire performance is analyzed and then top folks are selected&lt;/strong&gt;. Those who get shortlisted gets an &lt;strong&gt;&lt;em&gt;acceptance letter&lt;/em&gt;&lt;/strong&gt; along with the time and date of commencement of the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Strategy
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6c1gcxccpn4rj66craxq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6c1gcxccpn4rj66craxq.jpg" alt="strategy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to see &lt;strong&gt;&lt;em&gt;good results,&lt;/em&gt;&lt;/strong&gt; you must &lt;strong&gt;&lt;em&gt;plan it properly&lt;/em&gt;&lt;/strong&gt;. With enough research and thorough understanding, you can clearly envision what's going to happen and what to do if its going to fail. After all, we are computer science students, we should look at the world from a different, systematic perspective right? Okay, so I am going to share with you &lt;strong&gt;the mindset&lt;/strong&gt; I followed which got me through all the phases in &lt;strong&gt;the first attempt&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Feel The Heat
&lt;/h3&gt;

&lt;p&gt;I firmly believe that humans are capable of performing things &lt;strong&gt;&lt;em&gt;way better&lt;/em&gt;&lt;/strong&gt; when they &lt;strong&gt;&lt;em&gt;feel the heat&lt;/em&gt;&lt;/strong&gt;. MLH is a &lt;strong&gt;highly competed internship&lt;/strong&gt;. The seat that you are vying for has an acceptance rate of &lt;strong&gt;2.5%&lt;/strong&gt;. No, the data is not to scare you rather, its to challenge you to push harder. Now that you are feeling the heat and in the right mindset, lets proceed further.&lt;/p&gt;

&lt;h3&gt;
  
  
  First Things First
&lt;/h3&gt;

&lt;p&gt;Before even applying to MLH, you must have a good understanding of the following technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Any Programming Language(s)&lt;/strong&gt; &lt;em&gt;excluding C, C++, BASH&lt;/em&gt;. Yes, even I was pretty much taken aback by this. &lt;strong&gt;Preferred:&lt;/strong&gt; Python, JAVA, JS, TypeScript.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Git/GitHub&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don’t have any good understanding of the aforementioned topics, I suggest you to spend some time learning it. I would give you some &lt;strong&gt;&lt;em&gt;cool resources at the end&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cracking the Application Process
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;first milestone&lt;/strong&gt; to be reached. Let's understand this from &lt;strong&gt;&lt;em&gt;MLH's perspective&lt;/em&gt;&lt;/strong&gt;. &lt;em&gt;Why are they conducting this Initial Application process?&lt;/em&gt; One reason would be to &lt;strong&gt;remove spurious or "copy-pasted" applications&lt;/strong&gt;. The key thing to consider here is, &lt;strong&gt;be authentic&lt;/strong&gt;. Trust me on this. &lt;strong&gt;Many of my colleagues who went after the shortcut ways of filling application ended up receiving the same "Apply next time" letters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Write essays that &lt;strong&gt;&lt;em&gt;reflects you, in a professional way&lt;/em&gt;&lt;/strong&gt;. Use &lt;strong&gt;good amount of vocabulary&lt;/strong&gt; (something we never even really cared). Use big words, no really, I am not trying to be &lt;strong&gt;&lt;em&gt;ostentatious&lt;/em&gt;&lt;/strong&gt; 😃. This would, overall, &lt;strong&gt;make your application top-notch&lt;/strong&gt;. The reviewers would feel as if you have done hard work. A &lt;strong&gt;&lt;em&gt;sweet psychological&lt;/em&gt;&lt;/strong&gt; trick ain't it?&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;choice based questions&lt;/strong&gt;, choose things that are &lt;strong&gt;&lt;em&gt;applicable to you&lt;/em&gt;&lt;/strong&gt;. The more the merrier is &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; applicable here. In my application, I only opted for Python as the preferred Programming language (even though I knew around 3-4 others). &lt;strong&gt;&lt;em&gt;Be simple, be genuine and be professional.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There would be one section, &lt;strong&gt;&lt;em&gt;where you would have to describe a project that you are going to submit as a code sample&lt;/em&gt;&lt;/strong&gt;. One &lt;strong&gt;interesting observation&lt;/strong&gt; I made here was that &lt;strong&gt;web-based projects&lt;/strong&gt; were preferred over other ones. My colleagues gave projects like &lt;strong&gt;Library Management System, Hotel Booking Management System&lt;/strong&gt; etc. These projects are great from the beginner's standpoint but are not &lt;strong&gt;&lt;em&gt;multi disciplinary&lt;/em&gt;&lt;/strong&gt;. Projects like &lt;strong&gt;Full stack website, Simple Chat application&lt;/strong&gt;, even &lt;strong&gt;Calendar&lt;/strong&gt; shows that you have a &lt;strong&gt;&lt;em&gt;varied understanding of several topics&lt;/em&gt;&lt;/strong&gt;. Go for such projects if you haven't already and if you are already working on such projects, then great! You are on the right track.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cracking the Interviews
&lt;/h3&gt;

&lt;p&gt;Once you clear the first round, you are in for &lt;strong&gt;&lt;em&gt;a tougher fight&lt;/em&gt;&lt;/strong&gt;. Here, all the candidates who passed the application process are interviewed. This is a phase where many candidates loose focus and starts doubting.&lt;/p&gt;

&lt;p&gt;Let's again understand why interviews are conducted from the &lt;strong&gt;&lt;em&gt;MLH's standpoint&lt;/em&gt;&lt;/strong&gt;. Well, one obvious reason would be to &lt;em&gt;check whether you are a good fit or not&lt;/em&gt;. &lt;em&gt;Whether you have &lt;/em&gt;&lt;em&gt;it&lt;/em&gt;&lt;em&gt; in you to complete the internship&lt;/em&gt;. Keeping these in mind, its time to oil our guns.&lt;/p&gt;

&lt;p&gt;For &lt;strong&gt;initial interview&lt;/strong&gt;, being in the &lt;strong&gt;&lt;em&gt;right mindset&lt;/em&gt;&lt;/strong&gt; is really important. Let's look at this way: The interviewer has to interview many candidates. So, it is quite certain that there would be many questions he/she would ask that are &lt;strong&gt;&lt;em&gt;common for all&lt;/em&gt;&lt;/strong&gt;. Be prepared for these questions. The questions are going to be from &lt;strong&gt;your application form and some general questions&lt;/strong&gt; I mentioned way earlier in this post. The key thing to demonstrate is your &lt;strong&gt;&lt;em&gt;calm and composed attitude&lt;/em&gt;&lt;/strong&gt;. Believe me, its the easiest and the most interesting interview you'd have ever given so, &lt;em&gt;enjoy the process&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Technical interview, however requires some &lt;strong&gt;&lt;em&gt;grinding&lt;/em&gt;&lt;/strong&gt;. In this process, you would be given &lt;strong&gt;&lt;em&gt;most of the control&lt;/em&gt;&lt;/strong&gt; to demonstrate and give a brief walkthrough of the code sample you have submitted. It's &lt;strong&gt;&lt;em&gt;you&lt;/em&gt;&lt;/strong&gt; who has the complete authority. As you start explaining your project, follow this paradigm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Describe briefly about the project&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Tech stacks used&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Start explaining the project structure&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Start explaining the concepts&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The interviewer &lt;strong&gt;&lt;em&gt;is not going to look for the number of lines or files in your project rather, he/she is interested in finding out how deep you know a certain concept&lt;/em&gt;&lt;/strong&gt;. If the project is solely yours, then no need to fear at all. You would easily be able to give answer to the questions.&lt;/p&gt;

&lt;p&gt;You are expected to be an individual with &lt;strong&gt;strong fundamentals.&lt;/strong&gt; If you are a Python programmer, consider following this &lt;strong&gt;&lt;a href="https://www.youtube.com/playlist?list=PLbsliZj8JocIez5TvsbMaVKgF-sxoal0C" rel="noopener noreferrer"&gt;playlist&lt;/a&gt;&lt;/strong&gt;. Pick a topic that you don't know and start learning.&lt;/p&gt;

&lt;p&gt;I used a &lt;strong&gt;&lt;em&gt;neat trick&lt;/em&gt;&lt;/strong&gt; to trap my interviewer. While explaining my project, I gave emphasis on concepts like &lt;strong&gt;decorators, ORMs, Flask, Object Oriented Programming&lt;/strong&gt; among many that I was &lt;strong&gt;&lt;em&gt;dead comfortable with&lt;/em&gt;&lt;/strong&gt;. By doing this, I took him to &lt;strong&gt;&lt;em&gt;my territory&lt;/em&gt;&lt;/strong&gt; and guess what, the interviewer asked the questions related to those same concepts. &lt;strong&gt;&lt;em&gt;You can also do this, but only when your fundamentals are quite robust and have something to fall back on&lt;/em&gt;&lt;/strong&gt;. The aim here, is to &lt;strong&gt;prove your interviewer that you are one of the top folks.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Anticipating the Final Result
&lt;/h3&gt;

&lt;p&gt;If you have followed the above methods &lt;strong&gt;systematically&lt;/strong&gt;, then no need to worry at all. Consider &lt;strong&gt;&lt;em&gt;yourself selected*&lt;/em&gt;&lt;/strong&gt;. In fact, I was pretty confident that the moment after my technical interview got over, I notified my colleagues that I am going to be selected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;"A perfect amalgamation of psychology, smart work and dedication can take you a long way."&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you get selected and followed my techniques, then be sure to tell me about &lt;strong&gt;&lt;em&gt;your story&lt;/em&gt;&lt;/strong&gt;! Would love to hear it. You can mail me at: &lt;strong&gt;&lt;em&gt;&lt;a href="mailto:pratheekastro3@gmail.com"&gt;pratheekastro3@gmail.com&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt; or follow me at &lt;strong&gt;&lt;a href="//www.github.com/P0intMaN"&gt;GitHub&lt;/a&gt;&lt;/strong&gt; or connect with me on &lt;strong&gt;&lt;a href="https://www.linkedin.com/in/pratheek-u-7769a6190/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;These are some of the cool resources I found helpful during preparation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.youtube.com/playlist?list=PLbsliZj8JocIez5TvsbMaVKgF-sxoal0C" rel="noopener noreferrer"&gt;Python Programming Playlist&lt;/a&gt;&lt;/strong&gt; : Pick a concept, start learning&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.youtube.com/playlist?list=PLbsliZj8JocK-tz3bR8LVp_ZdN0CqG15b" rel="noopener noreferrer"&gt;Linux Fundamentals&lt;/a&gt;&lt;/strong&gt; : Understand Linux structure and basics&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.youtube.com/playlist?list=PLbsliZj8JocK6vYXvm4Yn2ftfvxGgutQV" rel="noopener noreferrer"&gt;Git Essentials&lt;/a&gt;&lt;/strong&gt; : A comprehensive Git guide&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.youtube.com/channel/UC1QZPervOHLiC4xpVnzbDFg" rel="noopener noreferrer"&gt;YouTube Channel&lt;/a&gt;&lt;/strong&gt; : A channel (relatively new) which aims at promoting contents related to computer science.&lt;/p&gt;

</description>
      <category>mlh</category>
      <category>programming</category>
      <category>github</category>
    </item>
    <item>
      <title>Alternative Constructors in Python</title>
      <dc:creator>P0intMaN</dc:creator>
      <pubDate>Fri, 29 Oct 2021 15:27:43 +0000</pubDate>
      <link>https://forem.com/p0intman/alternative-constructors-in-python-58gb</link>
      <guid>https://forem.com/p0intman/alternative-constructors-in-python-58gb</guid>
      <description>&lt;h2&gt;
  
  
  Article Agenda
&lt;/h2&gt;

&lt;p&gt;This post is dedicated towards understanding how we can create &lt;strong&gt;alternative constructors&lt;/strong&gt; in Python. Also, we would be taking a look at a real developmental scenario where I felt comfortable using it. To cut this short, we would be covering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constructors&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Alternative Constructors&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A Real Application Example&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Python OOPs has rocked the world with its &lt;strong&gt;simple yet stunning&lt;/strong&gt; flow of programming. Almost all the programs and software written in Python follow the OOPs paradigm. The OOP in Python is so modernized and enhanced that huge amount of developers are making a shift towards this amazing Programming language.&lt;/p&gt;

&lt;p&gt;In case you are wondering what an OOP is, I already have a &lt;strong&gt;&lt;a href="https://dev.to/p0intman/a-primer-on-object-oriented-programming-with-python-231g"&gt;Python OOP&lt;/a&gt;&lt;/strong&gt; post on this, which gives you a brief overview about it. You may want to give it a thorough read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constructors
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffoohjioacc2sj090jvw2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffoohjioacc2sj090jvw2.png" alt="constructors"&gt;&lt;/a&gt;&lt;br&gt;
Constructors by definition, is a &lt;strong&gt;subroutine designed to create an object in a particular class&lt;/strong&gt;. In layman's terms, a &lt;em&gt;method&lt;/em&gt;, which gets &lt;strong&gt;automatically called at the time of object creation and assists it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Constructors are like ordinary methods defined inside the class. The only difference being, we need &lt;strong&gt;not&lt;/strong&gt; call it explicitly. Programming languages automatically calls it while creating the object of the class.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;C++&lt;/strong&gt;, constructors have the &lt;strong&gt;same name&lt;/strong&gt; as the class name. For example (I know this is a Python tutorial, but I really felt the urge to show you the difference. So, here you go):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;          &lt;span class="c1"&gt;// Class name&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&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;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Constructor. Notice how its name is same as class name&lt;/span&gt;
    &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// I will be automatically executed during instantiation&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;MyClass&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// instantiation.&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Python, this is &lt;strong&gt;not&lt;/strong&gt; the case. However, there is a &lt;em&gt;huge misconception&lt;/em&gt; out there:&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;__init__()&lt;/code&gt; vs &lt;code&gt;__new__()&lt;/code&gt;. Which one is the real constructor?
&lt;/h3&gt;

&lt;p&gt;As opposed to a popular yet &lt;strong&gt;wrong&lt;/strong&gt; belief where &lt;strong&gt;&lt;code&gt;__init__()&lt;/code&gt; method is considered to be constructor, its actually the &lt;code&gt;__new__()&lt;/code&gt; method which is the constructor of the class&lt;/strong&gt;. To put it more clearly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;__new__()&lt;/code&gt;&lt;/strong&gt; : This method is called automatically to &lt;strong&gt;control&lt;/strong&gt; the object creation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;__init__()&lt;/code&gt;&lt;/strong&gt; : This method is also called automatically during object creation, but this is more of an &lt;strong&gt;initializer method&lt;/strong&gt; which initializes the object attributes. That's why we used it in our OOPs concept (an earlier post), to initialize our object attributes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moreover, if both &lt;strong&gt;&lt;code&gt;__new__()&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;__init__()&lt;/code&gt;&lt;/strong&gt; methods exist in the same class, then &lt;strong&gt;&lt;code&gt;__new__()&lt;/code&gt;&lt;/strong&gt; is called first and then Python interpreter decides whether to call &lt;strong&gt;&lt;code&gt;__init__()&lt;/code&gt;&lt;/strong&gt; or not. &lt;/p&gt;

&lt;p&gt;So, from the OOP standpoint, and from the above observations, it can be safely concluded that &lt;strong&gt;&lt;code&gt;__new__()&lt;/code&gt;&lt;/strong&gt; is the &lt;strong&gt;real constructor&lt;/strong&gt;. But several Devs just want to stay out of this pandemonium. Therefore, instead of going after this &lt;strong&gt;&lt;em&gt;"init vs new thing"&lt;/em&gt;&lt;/strong&gt;, they adopted another way to create constructors, enter the &lt;em&gt;Alternative Constructors&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative Constructors
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4oebat8xj8gbtvoimvd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4oebat8xj8gbtvoimvd.png" alt="ac"&gt;&lt;/a&gt;&lt;br&gt;
Alternative constructors are actually &lt;strong&gt;class methods&lt;/strong&gt; which serves the purpose of object creation. We hack the &lt;strong&gt;class methods&lt;/strong&gt; and command them to control the object creation. This is pretty similar to a constructor's (&lt;code&gt;__new__()&lt;/code&gt;'s) working. &lt;/p&gt;

&lt;p&gt;There is a convention for naming methods to be used as alternative constructors. All such methods should start with &lt;strong&gt;&lt;code&gt;from_&lt;/code&gt;&lt;/strong&gt;. For example, if I have a method say, &lt;strong&gt;&lt;code&gt;getDetails()&lt;/code&gt;&lt;/strong&gt; and if I were to make it an alternative constructor, then I would have to rename it as &lt;strong&gt;&lt;code&gt;from_getDetails()&lt;/code&gt;&lt;/strong&gt;. Although this is not necessary, but it is considered to be a good practice.&lt;/p&gt;

&lt;p&gt;The following snippet shows the basic syntax of defining alternative constructors (AC):&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;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;from_alternativeConstructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;      &lt;span class="c1"&gt;# Alternative Constructor
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                           &lt;span class="c1"&gt;# returns object
&lt;/span&gt;
&lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_alternativeConstructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# calling AC
&lt;/span&gt;
&lt;span class="c1"&gt;# an object is crafted successfully due to the execution of AC
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A Real Application Example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92dh8csug5o5sgg96e31.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92dh8csug5o5sgg96e31.png" alt="EXAMPLE"&gt;&lt;/a&gt;&lt;br&gt;
While I was developing a backend, I had a long list of strings. I had to come up with a way to convert these &lt;strong&gt;strings to objects&lt;/strong&gt;. Luckily, I had AC by my side and the task went on smoothly. In real world, you would get raw data like these. You would have to circumvent these obstacles and find a solution. &lt;/p&gt;

&lt;p&gt;The following snippet shows a demonstration of how I converted a long list of strings (but here, for the sake of simplicity, lets take a single string) and then converted it into objects.&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;Professor&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="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;age&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="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;

    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;from_getDetails&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;      &lt;span class="c1"&gt;# Alternative Constructor
&lt;/span&gt;        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# split string and assign to variables
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# returns object
&lt;/span&gt;
&lt;span class="n"&gt;details&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Jack Robins,2233394,45&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;prof&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_getDetails&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;details&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="n"&gt;prof&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;prof&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;prof&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# prints --&amp;gt; "Jack Robins" 2233394 45
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The string is now converted to an object and I can easily access it using its attributes. So, this was all about alternative constructors. One of the most interesting ways to bypass the basic working system of OOPs and also, exploiting the most out of it. After all, we are programmers, its in our DNA to exploit stuff and get things working for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Would You Like to Support Me?
&lt;/h2&gt;

&lt;p&gt;If you want to support me and my contents, then go ahead and consider doing it. I would highly appreciate that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/channel/UC1QZPervOHLiC4xpVnzbDFg" rel="noopener noreferrer"&gt;YouTube Channel&lt;/a&gt;: Home to all sorts of &lt;strong&gt;peculiar&lt;/strong&gt; tutos.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.github.com/P0intMaN" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;: Looking forward to your PR 😉&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>All You Need to Know About Closures in Python.</title>
      <dc:creator>P0intMaN</dc:creator>
      <pubDate>Thu, 28 Oct 2021 06:35:59 +0000</pubDate>
      <link>https://forem.com/p0intman/all-you-need-to-know-about-closures-in-python-pn0</link>
      <guid>https://forem.com/p0intman/all-you-need-to-know-about-closures-in-python-pn0</guid>
      <description>&lt;h2&gt;
  
  
  Article Agenda
&lt;/h2&gt;

&lt;p&gt;In this article, I will explain you the concept of &lt;strong&gt;Closures in Python&lt;/strong&gt;. More specifically, we will explore the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nested Functions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Closure Property&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Now, there's a &lt;strong&gt;caveat&lt;/strong&gt; to this post and that is: You must have a knowledge of &lt;strong&gt;First Class Functions&lt;/strong&gt;. If you don't know about this, then no need to worry. I got your back. Just view my article on &lt;a href="https://dev.to/p0intman/what-are-first-class-functions-in-python-213k"&gt;First Class Function&lt;/a&gt; or if you are more of an &lt;strong&gt;observational learner&lt;/strong&gt;, then you may go ahead and watch the &lt;a href="https://www.youtube.com/watch?v=EBtjFddFvSI"&gt;video version&lt;/a&gt; of First Class Function as well.&lt;/p&gt;

&lt;h6&gt;
  
  
  Psst.. If you are in a hurry, you can watch the &lt;a href="https://www.youtube.com/watch?v=bWpm80GzY6w"&gt;video version&lt;/a&gt; of this post instead
&lt;/h6&gt;

&lt;p&gt;Alright, let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XQncreBK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t2fh3bbtrjnw35olbch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XQncreBK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t2fh3bbtrjnw35olbch.png" alt="nested functions" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
Before we understand Closures, it is really important to know about &lt;strong&gt;nested functions&lt;/strong&gt;. Essentially, when you &lt;strong&gt;define one function inside another function, the resultant is called Nested function&lt;/strong&gt;. The following code snippet shows a nested function:&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;def&lt;/span&gt; &lt;span class="nf"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# 20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, I have a function &lt;strong&gt;&lt;code&gt;calculation()&lt;/code&gt;&lt;/strong&gt; which takes in a &lt;strong&gt;&lt;code&gt;number&lt;/code&gt;&lt;/strong&gt; as argument. Inside it, I have declared another function &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt; which also takes in an argument &lt;strong&gt;&lt;code&gt;no&lt;/code&gt;&lt;/strong&gt; and then returns the double of &lt;strong&gt;&lt;code&gt;no&lt;/code&gt;&lt;/strong&gt;. Finally, I exited from the &lt;strong&gt;&lt;code&gt;calculation()&lt;/code&gt;&lt;/strong&gt; function by making a call to the &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;Now the above scenario was a classical example of a nested function. Let's understand each step briefly:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A call is made to &lt;strong&gt;&lt;code&gt;calculation()&lt;/code&gt;&lt;/strong&gt; with parameter value &lt;strong&gt;&lt;code&gt;10&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Interpreter &lt;strong&gt;jumps inside &lt;code&gt;calculation()&lt;/code&gt;&lt;/strong&gt; and finds a &lt;strong&gt;&lt;code&gt;return&lt;/code&gt;&lt;/strong&gt; statement with call to another function &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt; with the same parameter &lt;strong&gt;&lt;code&gt;number = 10&lt;/code&gt;&lt;/strong&gt;. Current state is &lt;strong&gt;paused&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Interpreter &lt;strong&gt;jumps inside &lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt; with parameter &lt;strong&gt;&lt;code&gt;no = 10&lt;/code&gt;&lt;/strong&gt; and then returns the double of &lt;strong&gt;&lt;code&gt;no&lt;/code&gt;&lt;/strong&gt; which is &lt;strong&gt;&lt;code&gt;20&lt;/code&gt;&lt;/strong&gt;. Exits the &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt; function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;20&lt;/code&gt;&lt;/strong&gt; gets reflected and the &lt;strong&gt;paused state is resumed&lt;/strong&gt;. This results in us finally getting the value &lt;strong&gt;&lt;code&gt;20&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Great! See, how easy it was? Okay, lets cover a scenario where you have &lt;strong&gt;multiple nested functions inside a parent function&lt;/strong&gt;. In this case, &lt;strong&gt;all those functions will be executed whose call have been made in the parent function's &lt;code&gt;return&lt;/code&gt; statement&lt;/strong&gt;. The following example demonstrates it:&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;def&lt;/span&gt; &lt;span class="nf"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;           &lt;span class="c1"&gt;# executed
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;triple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;           &lt;span class="c1"&gt;# skipped
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;quadruple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;        &lt;span class="c1"&gt;# executed
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;quadruple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# (20, 40) &amp;lt;-- this is a tuple
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very well. Now this was all about Nested Functions. Now, what if I tell you that there is a &lt;strong&gt;neater way&lt;/strong&gt; of writing the nested functions? There exists a &lt;strong&gt;more professional and a cleaner way&lt;/strong&gt; of writing nested functions and that way makes use of a property called &lt;strong&gt;&lt;em&gt;Closure&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure Property
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lv0PYlQz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuhs41yyuluqchkd0q7t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lv0PYlQz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuhs41yyuluqchkd0q7t.png" alt="Closure property" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
Closure property (simply closure) by definition, are &lt;strong&gt;&lt;em&gt;function objects that can remember values in its enclosing scopes and these values can be used within that function&lt;/em&gt;&lt;/strong&gt;. I know this might've gone way over your head, so let's understand it bit more clearly.&lt;/p&gt;

&lt;p&gt;First of all, let's see what an &lt;strong&gt;enclosing scope&lt;/strong&gt; means:&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;def&lt;/span&gt; &lt;span class="nf"&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&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;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, &lt;strong&gt;&lt;code&gt;parent&lt;/code&gt; is the enclosing scope for &lt;code&gt;child&lt;/code&gt;&lt;/strong&gt;. Which means that &lt;strong&gt;&lt;code&gt;child&lt;/code&gt;&lt;/strong&gt; will only be invoked if &lt;strong&gt;&lt;code&gt;parent&lt;/code&gt;&lt;/strong&gt; makes a call for it. Otherwise, &lt;strong&gt;&lt;code&gt;child&lt;/code&gt;&lt;/strong&gt; will stay dormant.&lt;/p&gt;

&lt;p&gt;Now, let's see what does &lt;strong&gt;&lt;em&gt;"values inside the enclosing scope"&lt;/em&gt;&lt;/strong&gt; mean:&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="n"&gt;outside1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Outside parent func"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;inside1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I am inside parent"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

    &lt;span class="n"&gt;inside2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I am inside parent"&lt;/span&gt;

    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="n"&gt;outside2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Outside parent func"&lt;/span&gt;

&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello there!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, &lt;strong&gt;&lt;code&gt;outside1&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;outside2&lt;/code&gt;&lt;/strong&gt; are not inside &lt;strong&gt;&lt;code&gt;parent()&lt;/code&gt;&lt;/strong&gt; therefore, they are &lt;strong&gt;not&lt;/strong&gt; inside the enclosing scope of &lt;strong&gt;&lt;code&gt;child()&lt;/code&gt;&lt;/strong&gt;. On the other hand, &lt;strong&gt;&lt;code&gt;inside1&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;inside2&lt;/code&gt;&lt;/strong&gt; are inside &lt;strong&gt;&lt;code&gt;parent()&lt;/code&gt;&lt;/strong&gt; function, this means that they are &lt;strong&gt;within&lt;/strong&gt; the enclosing scope of &lt;strong&gt;&lt;code&gt;child()&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;strong&gt;&lt;em&gt;can you find one more value (or variable) which is inside the enclosing scope of &lt;code&gt;child()&lt;/code&gt;?&lt;/em&gt;&lt;/strong&gt; Yes, the &lt;strong&gt;&lt;code&gt;msg&lt;/code&gt;&lt;/strong&gt; variable. Since we can use &lt;strong&gt;&lt;code&gt;msg&lt;/code&gt;&lt;/strong&gt; inside the &lt;strong&gt;&lt;code&gt;parent()&lt;/code&gt;&lt;/strong&gt; function, we can successfully  call it to be in the enclosing scope of &lt;strong&gt;&lt;code&gt;child()&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Finally, let's understand what does &lt;strong&gt;&lt;em&gt;"remembers the value in enclosing scope"&lt;/em&gt;&lt;/strong&gt; mean. Let's take our previous example and move on from there:&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;def&lt;/span&gt; &lt;span class="nf"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;number2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;             

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="n"&gt;number3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# 20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have added two extra variables as well, for better understanding. Okay, so here, as per our previous observations, we can quickly conclude that, &lt;strong&gt;&lt;code&gt;number&lt;/code&gt;, &lt;code&gt;number2&lt;/code&gt;, &lt;code&gt;number3&lt;/code&gt;&lt;/strong&gt; are in the enclosing scope of &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt;. Now, let me remind you, closure property states that: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;function objects can remember values in its enclosing scopes and these values can be used within that function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that all these variables in the enclosing scope of &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt;&lt;/strong&gt; can easily be &lt;strong&gt;accessed inside double&lt;/strong&gt; and be used in any way you want &lt;strong&gt;without even passing them as the parameters to &lt;code&gt;double()&lt;/code&gt; function&lt;/strong&gt;. Didn't get this? Check out the valid snippet below:&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;def&lt;/span&gt; &lt;span class="nf"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;number2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;             

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"I can remember: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;number2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;number3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;number3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# I can remember: 10,30,50
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that how &lt;strong&gt;&lt;code&gt;double()&lt;/code&gt; can access and modify &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;number2&lt;/code&gt;, &lt;code&gt;number3&lt;/code&gt; without them being passed as arguments&lt;/strong&gt;. Well, this is the due to it being able to &lt;strong&gt;&lt;em&gt;"remember"&lt;/em&gt;&lt;/strong&gt; the values in its enclosing scope. This was the great story of &lt;strong&gt;Closure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, before ending this article, lets refactor our previous cluttered code that we wrote without closures. You would be seeing a &lt;strong&gt;significant difference&lt;/strong&gt; in the style.&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;def&lt;/span&gt; &lt;span class="nf"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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;double&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;           &lt;span class="c1"&gt;# executed
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;triple&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;           &lt;span class="c1"&gt;# skipped
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;quadruple&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;        &lt;span class="c1"&gt;# executed
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;quadruple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;calculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# (20, 40) &amp;lt;-- this is a tuple
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks neat ain't it? No tossing around of parameters and not even explicitly passing them. The code looks way more cleaner and professional.&lt;/p&gt;

&lt;h2&gt;
  
  
  Would You Like to Support Me?
&lt;/h2&gt;

&lt;p&gt;If you want to support me and my contents, then go ahead and consider doing it. I would highly appreciate that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/channel/UC1QZPervOHLiC4xpVnzbDFg"&gt;YouTube Channel&lt;/a&gt;: Home to all sorts of &lt;strong&gt;peculiar&lt;/strong&gt; tutos.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.github.com/P0intMaN"&gt;GitHub&lt;/a&gt;: Looking forward to your PR 😉 &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>What are First Class Functions in Python?</title>
      <dc:creator>P0intMaN</dc:creator>
      <pubDate>Wed, 27 Oct 2021 17:31:15 +0000</pubDate>
      <link>https://forem.com/p0intman/what-are-first-class-functions-in-python-213k</link>
      <guid>https://forem.com/p0intman/what-are-first-class-functions-in-python-213k</guid>
      <description>&lt;h2&gt;
  
  
  Article Agenda
&lt;/h2&gt;

&lt;p&gt;This article is focused at making you understand the property of functions to behave as &lt;strong&gt;first class objects&lt;/strong&gt;. We will cover the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;First Class Object&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;First Class Functions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What Next?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You might have heard of the famous saying, &lt;strong&gt;&lt;em&gt;"In Python, everything is an object"&lt;/em&gt;&lt;/strong&gt;. Very confusing as it may sound, this is &lt;strong&gt;absolutely true&lt;/strong&gt;. In Python, every single entity is represented as an object ( Yes, object from the OOP. In case you don't know about OOP, I have got it briefly covered in this &lt;a href="https://dev.to/p0intman/a-primer-on-object-oriented-programming-with-python-231g"&gt;article&lt;/a&gt; ). This concept has given rise to many methodologies and principles in Python like &lt;strong&gt;Closures and Decorators&lt;/strong&gt;. &lt;/p&gt;

&lt;h6&gt;
  
  
  Psst.. If you are in a hurry, you can watch the &lt;a href="https://www.youtube.com/watch?v=EBtjFddFvSI"&gt;video version&lt;/a&gt; of this post instead
&lt;/h6&gt;

&lt;p&gt;Alright, let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Class Object
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XIWEPJV0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tpipxdogqzf30kow4bk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XIWEPJV0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tpipxdogqzf30kow4bk.png" alt="fco" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
To keep it short and simple, A first-class object is an entity within a programming language that can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Appear in an expression&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Be assigned to a variable&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Be used as an argument&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Be returned by a function call&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, what do you think would be candidates to this? Well, &lt;strong&gt;Python datatypes&lt;/strong&gt; fit these criteria, so datatypes like Integer, String, Boolean etc are &lt;strong&gt;first-class objects&lt;/strong&gt;. So, let me ask: &lt;strong&gt;&lt;em&gt;Are loops and conditionals first-class objects?&lt;/em&gt;&lt;/strong&gt; Of course &lt;strong&gt;not&lt;/strong&gt; since they both are failing multiple criteria. Finally, we can achieve all the aforementioned with functions as well! Therefore, &lt;strong&gt;functions are also first-class objects.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  First Class Functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gdgw2QlK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo1083npvj0pibsmkpa9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gdgw2QlK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo1083npvj0pibsmkpa9.png" alt="FCF" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
In programming languages like Python, JavaScript, we have functions as First-Class Objects. Hence, the &lt;strong&gt;formal name of a function is First-Class Function&lt;/strong&gt; (although we just call it function). First-Class Functions can be treated as &lt;strong&gt;any other object&lt;/strong&gt;. This means that in Python, &lt;strong&gt;functions can be passed around, tossed in as arguments to other functions and even be assigned to variables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Given below is a function which increments a number by 1:&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;def&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to prove that functions can be treated as other objects, let me go ahead and rename this function like so:&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;def&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="c1"&gt;#renaming the above function
&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;plusOne&lt;/span&gt;

&lt;span class="c1"&gt;#calling both the functions
&lt;/span&gt;&lt;span class="n"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# 11
&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;# 11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'd notice that both of them gives you the same output. &lt;strong&gt;&lt;em&gt;Do you remember encountering this behavior elsewhere?&lt;/em&gt;&lt;/strong&gt; Well, if you run the following code, you might get an idea:&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# 11
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# 11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprising ain't it? Therefore, &lt;strong&gt;functions behave like the first-class objects&lt;/strong&gt;. This is something unique that other programming languages like C, C++ never had. Functions in Python can be treated as first class objects (String, Integer, etc).&lt;/p&gt;

&lt;p&gt;Now, I am going to spice things up. This might seem a bit weird in the beginning but you will understand it and grasp the concept pretty quick.&lt;/p&gt;

&lt;p&gt;If you remember that we can store the First Class Objects in some other data structure like Lists, Dictionaries right? Well, this means that a &lt;strong&gt;function too can be stored in these data structures and used accordingly&lt;/strong&gt;. Let me explain this more briefly (using List as the data structure):&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;def&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plusTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plusThree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plusFour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="c1"&gt;# list storing function objects
&lt;/span&gt;&lt;span class="n"&gt;func_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plusTwo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plusThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plusFour&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# executing each function
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;func_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# It will print out ---&amp;gt; 11, 12, 13, 14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet, I have created a &lt;code&gt;func_list&lt;/code&gt; list which will store the function objects. Now, before I move further, it is imperative to understand that Python &lt;strong&gt;does not make a call to the function unless you succeed it with brackets &lt;code&gt;()&lt;/code&gt;&lt;/strong&gt;. So, if I only do this:&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# prints out ---&amp;gt; &amp;lt;function plusOne at 0x000001B4FC7FEF70&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python would only give me the &lt;strong&gt;function reference&lt;/strong&gt; as shown above. Alright, let's go back. Now, the &lt;code&gt;func_list&lt;/code&gt; holds the function objects (which are not yet executed). Then, I ran a for loop, accessed each function and then &lt;strong&gt;finally executed it with the help of parenthesis&lt;/strong&gt; and &lt;strong&gt;a parameter, 10&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Great stuff! I believe now you got to know what the original behavior of functions are. To truly solidify your grasp on this concept, try to make use of it wherever you find its possible use case.&lt;/p&gt;

&lt;p&gt;Let me try and come up with a use case of first class functions:&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;def&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plusTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;listModifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;

&lt;span class="c1"&gt;# executing listModifier
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listModifier&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# prints out ---&amp;gt; [2,3,4,5]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet, I passed a function (&lt;code&gt;plusOne&lt;/code&gt;) as the &lt;strong&gt;parameter to another function&lt;/strong&gt; (&lt;code&gt;listModifier&lt;/code&gt;). Then, I made a call to &lt;code&gt;plusOne&lt;/code&gt; from &lt;strong&gt;within the &lt;code&gt;listModifier&lt;/code&gt; function&lt;/strong&gt;. This would trigger the &lt;code&gt;plusOne&lt;/code&gt; function and will increment each value of list passed and then append it to a &lt;code&gt;result&lt;/code&gt; list. Finally, after the above operation, simply &lt;strong&gt;returned&lt;/strong&gt; &lt;code&gt;result&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's suppose for some reason, the client comes to me and tells me that instead of incrementing each value of the element by 1, do it by 2, all I would have to do is &lt;strong&gt;simply replace the &lt;code&gt;plusOne&lt;/code&gt; parameter of &lt;code&gt;listModifier&lt;/code&gt; to &lt;code&gt;plusTwo&lt;/code&gt; like so&lt;/strong&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="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listModifier&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;plusTwo&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;   &lt;span class="c1"&gt;# pretty neat eh?
&lt;/span&gt;
&lt;span class="c1"&gt;# prints out ---&amp;gt; [3,4,5,6]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Next
&lt;/h2&gt;

&lt;p&gt;Let me tell you that, you have reached a &lt;strong&gt;major milestone&lt;/strong&gt; by learning this concept. First Class Function form the basis of several other &lt;strong&gt;design patterns and concepts such as Closures and Decorators&lt;/strong&gt;. You'd have a better understanding of those topics by having a knowledge of first class functions beforehand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Would You Like to Support Me?
&lt;/h2&gt;

&lt;p&gt;If you want to support me and my contents, then go ahead and consider doing it. I would highly appreciate that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/channel/UC1QZPervOHLiC4xpVnzbDFg"&gt;YouTube Channel&lt;/a&gt;: Home to all sorts of &lt;strong&gt;peculiar&lt;/strong&gt; tutos.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.github.com/P0intMaN"&gt;GitHub&lt;/a&gt;: Looking forward to your PR 😉 &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>A Primer on Object Oriented Programming with Python.</title>
      <dc:creator>P0intMaN</dc:creator>
      <pubDate>Wed, 27 Oct 2021 08:47:20 +0000</pubDate>
      <link>https://forem.com/p0intman/a-primer-on-object-oriented-programming-with-python-231g</link>
      <guid>https://forem.com/p0intman/a-primer-on-object-oriented-programming-with-python-231g</guid>
      <description>&lt;h2&gt;
  
  
  Article Agenda
&lt;/h2&gt;

&lt;p&gt;This article is focused at getting you up and running pretty quick on the concept of &lt;strong&gt;OOPs in Python&lt;/strong&gt;. More specifically, we will cover the following topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Classes&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instances&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Methods&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article is going to be a quite a bit lengthy since I will be giving you a proper explanation to each concepts that I am going to discuss. But I hope you will have a lot to take away form this.&lt;/p&gt;

&lt;h6&gt;
  
  
  There is another interesting follow up topic to OOP in Python, which is called &lt;strong&gt;inheritance&lt;/strong&gt;. This is a whole world in itself and for the sake of not converting this article into a full-fledged book, I decided to talk about it in another article. It deserves a dedicated article on its own.
&lt;/h6&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IrEJ_OT4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18g6prd16wt9ew21lbo6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IrEJ_OT4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18g6prd16wt9ew21lbo6.png" alt="Introduction" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
Python has been there in the computer science realm for a long time. Before Python, JAVA used to be the &lt;strong&gt;Big Thing&lt;/strong&gt; (It is still very popular, but not that much when compared to Python).&lt;/p&gt;

&lt;p&gt;One of the most desirable features of Python is Object Oriented Programming (OOP in short). OOP in Python is pretty neat and very organized. Developers really &lt;strong&gt;love the flow&lt;/strong&gt; while writing Python OOP.&lt;/p&gt;

&lt;p&gt;So, with that being said, lets quickly jump right in.&lt;/p&gt;
&lt;h2&gt;
  
  
  Object Oriented Programming
&lt;/h2&gt;

&lt;p&gt;In object-oriented programming, I realized the power of using classes and objects as a paradigm for writing codes. In fact, OOPs, as it is called, is all about using &lt;strong&gt;classes and objects&lt;/strong&gt;. One thing I noticed while using Python’s OOPs is that, its quite different from other programming languages like C, C++. &lt;strong&gt;Python’s OOPs is crisp and straight to the point&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Conceptually, objects are like the &lt;strong&gt;components of a system&lt;/strong&gt;. Think of a program as a factory assembly line of sorts. At each step of the assembly line a system component processes some material, ultimately transforming raw material into a finished product. An object contains data, like the raw or pre-processed materials at each step on an assembly line, and behavior, like the action each assembly line component performs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Class in Python OOPs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EHgnIwDi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yekqkt8j8t8vg0atw8rl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EHgnIwDi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yekqkt8j8t8vg0atw8rl.png" alt="class" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
To understand the concept of class, lets take an example. Suppose in &lt;strong&gt;&lt;em&gt;LPU&lt;/em&gt;&lt;/strong&gt; (a university), there are hundreds of professors. &lt;strong&gt;Each professors have their own credentials like name, age, id etc&lt;/strong&gt;. What sort of data type will you use to store such a vast data and manage them in a meaningful way? One quick workaround would be to use a list to represent them. For example:&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="n"&gt;ishaan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Ishaan'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;125&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;loviRaj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Lovi Raj'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;225&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Pooja Rana'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# nameofProfessor = [‘name’, age, id]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this mode of representation has a &lt;strong&gt;big limitation&lt;/strong&gt;. You’d need to create separate variables for all the professors. Also, lets suppose that a newly joined professor say, &lt;strong&gt;Pratheek&lt;/strong&gt; has not been assigned an id yet. Now, if you are managing a really big data like this one, you might not even realize that &lt;strong&gt;Pratheek&lt;/strong&gt; has &lt;strong&gt;no id yet&lt;/strong&gt;. So, generally, if you try to retrieve &lt;strong&gt;Pratheek’s id&lt;/strong&gt;, you might end up getting &lt;strong&gt;his age&lt;/strong&gt; as shown below:&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="n"&gt;shaan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Ishaan'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;125&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;loviRaj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Lovi Raj'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;225&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Pooja Rana'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Pratheek'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# id has not been given yet
&lt;/span&gt;
&lt;span class="c1"&gt;#retrieving ids
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# will give you pooja's id
&lt;/span&gt;&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# will give you pratheek's AGE (but needed his id)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is some sort of inconsistency. Therefore, we need a better way to handle such &lt;strong&gt;real-world data&lt;/strong&gt;. Here comes the class.&lt;/p&gt;

&lt;p&gt;Creating class is easy. It is shown below:&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;Myclass&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;Now, to solve our above problem using classes, first of all, we would have to declare a class with a useful name, say &lt;strong&gt;Professor&lt;/strong&gt;. Each class in python has its own attributes. These attributes are either the &lt;strong&gt;instance variables or the class variables.&lt;/strong&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Professor&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;It’s that simple. You have a class named Professor. Now, we will currently leave class as empty. I will fill this once I finish talking about the instances.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instance (Or Object) in Python OOPs
&lt;/h2&gt;

&lt;p&gt;Instances are &lt;strong&gt;ways through which one can assign&lt;/strong&gt; or &lt;strong&gt;retrieve information&lt;/strong&gt; from the class or perhaps even &lt;strong&gt;modify it&lt;/strong&gt;. &lt;strong&gt;Every instance in class is unique&lt;/strong&gt; having &lt;strong&gt;their own blueprints of the class&lt;/strong&gt;. So, if I create three instances of &lt;strong&gt;Professor&lt;/strong&gt; class, I would have blueprints of Professor’s class in &lt;strong&gt;each of these three instances and all the three instances&lt;/strong&gt; are &lt;strong&gt;independent&lt;/strong&gt; of each other.&lt;/p&gt;

&lt;p&gt;The syntax for instantiation is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;instanceName = className(inheritSomething[if any])&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s see this in action. In the following code, I have instantiated the class with an instance called &lt;strong&gt;prof&lt;/strong&gt;. Then, I have given the &lt;strong&gt;attributes (name, id, age)&lt;/strong&gt; of the class by using the instance &lt;strong&gt;prof&lt;/strong&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="n"&gt;prof&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# instantiated the class with prof instance
&lt;/span&gt;
&lt;span class="c1"&gt;# using prof instance to give class attributes
&lt;/span&gt;&lt;span class="n"&gt;prof&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ProfessorName"&lt;/span&gt; 
&lt;span class="n"&gt;prof&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;
&lt;span class="n"&gt;prof&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, with this, we have &lt;strong&gt;successfully registered an object&lt;/strong&gt; of Professor class. Now, lets take a look at the previous case where &lt;strong&gt;we don’t have Pratheek’s id&lt;/strong&gt;. So, in that case you don’t even have to pass the &lt;strong&gt;&lt;em&gt;id&lt;/em&gt; attribute&lt;/strong&gt;. Therefore, &lt;strong&gt;when the developer tries to retrieve Pratheek’s id&lt;/strong&gt;, he would be &lt;strong&gt;thrown an error&lt;/strong&gt; instead of giving him the &lt;strong&gt;age of Pratheek which is absolutely good since it did not fetch us the wrong value&lt;/strong&gt;. The below code demonstrates this scenario:&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;Professor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="c1"&gt;#instances
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;#assigning attributes
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Pooja Rana"&lt;/span&gt;
&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;
&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;

&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Pratheek"&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;

&lt;span class="c1"&gt;#retrieving the ids
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;                      &lt;span class="c1"&gt;# no error
&lt;/span&gt;&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;                   &lt;span class="c1"&gt;# error
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the previous &lt;strong&gt;no-class&lt;/strong&gt; scenario, we could have got an ambiguous value (mismatch of &lt;em&gt;age&lt;/em&gt; and &lt;em&gt;id&lt;/em&gt;), which would have gotten us into some serious troubles. &lt;strong&gt;Also, by using class, the code got a meaningful structure&lt;/strong&gt;. When you want to add a new attribute, just use an object and give an appropriate attribute name and you are done! That’s why Oops is really interesting to work with. &lt;/p&gt;

&lt;p&gt;Ok, so one thing I noticed in the above way of assigning attributes to instances is that, we would have to use the &lt;strong&gt;&lt;em&gt;dot&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;attribute name every time we would want to give a value&lt;/strong&gt;. This &lt;strong&gt;might get cumbersome&lt;/strong&gt; if you are having really large number of professors in the university. Also, &lt;strong&gt;this is a repetitive task&lt;/strong&gt;. Notice how &lt;strong&gt;&lt;em&gt;‘.name, .id, .age’&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;is repeated for all the instances?&lt;/strong&gt; Well, there is a neat way to circumvent this. It is, by using &lt;strong&gt;&lt;em&gt;init() method&lt;/em&gt;&lt;/strong&gt;. But before moving onto &lt;strong&gt;&lt;em&gt;init method&lt;/em&gt;&lt;/strong&gt;, lets talk about &lt;strong&gt;methods&lt;/strong&gt; for a moment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method in Python OOPs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QmE8hluh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrghzdxcnyh8q57cd2lp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QmE8hluh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrghzdxcnyh8q57cd2lp.png" alt="Methods" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like we make use of functions in Python, we can have functions inside class as well. &lt;strong&gt;Any function that you define inside class is no longer called a function, it’s called a method. Every method inside a class must’ve it’s first argument as &lt;em&gt;self&lt;/em&gt;.&lt;/strong&gt; There is a reason to it. Which I will be demonstrating in the upcoming code snippet. We can call &lt;strong&gt;the function from outside the class using the instance name&lt;/strong&gt;. The following code snippet demonstrates the methods concept of Python:&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;Professor&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;displayName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#instances
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# assigning attributes
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Pooja"&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Pratheek"&lt;/span&gt;

&lt;span class="c1"&gt;# calling method
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;       &lt;span class="c1"&gt;# Pooja
&lt;/span&gt;&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;    &lt;span class="c1"&gt;# Pratheek
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, notice how the displayName gave different outputs for both &lt;strong&gt;Pratheek and Pooja&lt;/strong&gt;. Well, that’s because as I mentioned earlier in this report that, whenever you create an instance, it gets an independent blueprint of the class. Therefore, &lt;strong&gt;the attributes of Pratheek are in no way having control over Pooja’s&lt;/strong&gt;. Hence, we got two outputs.&lt;/p&gt;

&lt;p&gt;Okay, lets talk about that &lt;strong&gt;&lt;em&gt;self&lt;/em&gt;&lt;/strong&gt; keyword. Now, &lt;strong&gt;by default, whenever you call a method,&lt;/strong&gt; in the backend, python &lt;strong&gt;does this conversation&lt;/strong&gt; implicitly:&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="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;methodName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instanceVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore, &lt;strong&gt;&lt;em&gt;pooja.displayName()&lt;/em&gt;&lt;/strong&gt; is converted implicitly to:&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="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, as you can see that &lt;strong&gt;automatically, python passes the instance as its first argument&lt;/strong&gt;. Therefore, to receive this argument, we use the keyword  &lt;strong&gt;&lt;em&gt;self&lt;/em&gt;&lt;/strong&gt;, although you can use &lt;strong&gt;any word of your choice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let me take you back to the &lt;strong&gt;&lt;em&gt;init method&lt;/em&gt;&lt;/strong&gt;. Init method is &lt;strong&gt;used to initialize attributes to a given object&lt;/strong&gt;. So, instead of writing &lt;strong&gt;&lt;em&gt;object dot attributename&lt;/em&gt;&lt;/strong&gt; every time, we can pass &lt;br&gt;
those &lt;strong&gt;attributes values as the parameters to the class name when we declare an instance&lt;/strong&gt;. &lt;br&gt;
The following code demonstrates the usage of &lt;strong&gt;&lt;em&gt;init method&lt;/em&gt;&lt;/strong&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Professor&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#instances
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pooja'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pratheek'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#calling methods
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;        &lt;span class="c1"&gt;# Pooja
&lt;/span&gt;&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;     &lt;span class="c1"&gt;# Pratheek
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code looks &lt;strong&gt;more organized now&lt;/strong&gt;. See how I’ve passed the attribute values while I declared the object. Also, &lt;strong&gt;&lt;em&gt;id&lt;/em&gt; has become a mandatory field now&lt;/strong&gt;. Since Pratheek doesn’t have an id yet, I assigned &lt;strong&gt;&lt;em&gt;None&lt;/em&gt;&lt;/strong&gt; as its &lt;strong&gt;&lt;em&gt;id&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In python, we do have other method types apart from these normal methods and they are:&lt;br&gt;
&lt;strong&gt;&lt;code&gt;class methods&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;static methods&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Methods vs Class Methods vs Static Methods
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S2v2lGpW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bztxf27ucvenx0wsis9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S2v2lGpW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bztxf27ucvenx0wsis9y.png" alt="ClassvsStatic" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
In this sub section, we will be covering class and static methods. Now, before starting off with these, its imperative to understand the difference between &lt;strong&gt;class and instance variables&lt;/strong&gt;. Class variable is &lt;strong&gt;shared across all the instances&lt;/strong&gt; while &lt;strong&gt;instance variables are specific&lt;/strong&gt; and is unique for that instance only. Class variables are &lt;strong&gt;declared inside class and outside any methods&lt;/strong&gt;. Instance variables are &lt;strong&gt;declared inside &lt;em&gt;init method&lt;/em&gt;&lt;/strong&gt;. The following code snippet shows class and instance variables:&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;Professor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;classvar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"this is a class variable"&lt;/span&gt; &lt;span class="c1"&gt;# class variable
&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;      &lt;span class="c1"&gt;# instance variable
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;       &lt;span class="c1"&gt;# instance variable
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;        &lt;span class="c1"&gt;# instance variable
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#instances
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pooja'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pratheek'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet, &lt;strong&gt;classvar&lt;/strong&gt; is common variable to all the instances (if you explicitly assign it or not). Therefore, even though we did not mention about classvar in pooja and pratheek, they &lt;strong&gt;both will contain a class variable called classvar&lt;/strong&gt; which can be accessed and used whichever way you’d like.&lt;/p&gt;

&lt;p&gt;Now, lets talk about the &lt;strong&gt;classmethods&lt;/strong&gt;. Generally, class methods are used to modify, edit or even delete the class variables. Just like a normal method, it also takes a first argument by default but only in this case, it takes &lt;strong&gt;class name as its first argument&lt;/strong&gt;. Also, whenever you are declaring a class method, you should enclose it using a &lt;strong&gt;&lt;em&gt;@classmethod decorator&lt;/em&gt;&lt;/strong&gt;. This is done to show Python that the method defined here is a class method and should not be mistaken with the normal method. &lt;strong&gt;Class methods are usually called using the class name&lt;/strong&gt;. The following code snippet demonstrates the use of &lt;strong&gt;classmethod&lt;/strong&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;classvar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"this is a class variable"&lt;/span&gt; &lt;span class="c1"&gt;# class variable
&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;      &lt;span class="c1"&gt;# instance variable
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;       &lt;span class="c1"&gt;# instance variable
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;        &lt;span class="c1"&gt;# instance variable
&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;#instances
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pooja'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pratheek'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# calling the class method
&lt;/span&gt;&lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'LPU Dean'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# LPU Dean
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pratheek&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# LPU Dean
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet, you can see that although &lt;strong&gt;I haven’t declared dean attribute for pooja and pratheek, but still it got reflected later as their attributes&lt;/strong&gt;. This is one of the most common use case of &lt;strong&gt;class methods&lt;/strong&gt;. They are used to set / modify class variables. You could also use instances to set the class variable, but it would be a vague or unsuitable candidate and also, I can’t really think of a case where an object can decide what class variable should the class hold.&lt;/p&gt;

&lt;p&gt;Now, comes the &lt;strong&gt;static method&lt;/strong&gt;. Static method, in general, does not serve other purposes rather than to use &lt;strong&gt;them as logical connectors&lt;/strong&gt; which means that it is used to implement &lt;strong&gt;some sort of logic within the class and does not affect the instances in any way.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Static methods &lt;strong&gt;do not&lt;/strong&gt; take any default arguments. You can declare a static method using the &lt;strong&gt;@staticmethod decorator&lt;/strong&gt;. It can be called using the &lt;strong&gt;class name or instance name&lt;/strong&gt;. The following snippet shows its usage:&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;Professor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;classvar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"this is a class variable"&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;      
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;       
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;        

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;staticmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;displayPrompt&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The details have been entered"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#instances
&lt;/span&gt;&lt;span class="n"&gt;pooja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pooja'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pooja&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayPrompt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;       &lt;span class="c1"&gt;# calling via instance
&lt;/span&gt;
&lt;span class="n"&gt;pratheek&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pratheek'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Professor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;displayPrompt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;# calling via class name
&lt;/span&gt;
&lt;span class="c1"&gt;# both prints out the same result.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, both &lt;code&gt;displayPrompt()&lt;/code&gt; will output the same result. Also, notice that in it, we haven’t passed any default arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  The End is &lt;em&gt;Not&lt;/em&gt; Nigh
&lt;/h2&gt;

&lt;p&gt;We discussed a lot in this article. You are now well equipped with the fundamentals to take on this OOPs fight. In future, I will include some Inheritance articles and even some extras that many don't know about OOPs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Would You Like to Support Me?
&lt;/h2&gt;

&lt;p&gt;If you want to support me and my contents, then go ahead and consider doing it. I would highly appreciate that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/channel/UC1QZPervOHLiC4xpVnzbDFg"&gt;YouTube Channel&lt;/a&gt;: Home to all sorts of &lt;strong&gt;peculiar&lt;/strong&gt; tutos.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.github.com/P0intMaN"&gt;GitHub&lt;/a&gt;: Looking forward to your PR 😉 &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>oop</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
