[
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.md",
    "content": "---\nname: Bug report\nabout: Create a report to help us improve\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n\n**Describe the bug**\nA clear and concise description of what the bug is.\n\n**To Reproduce**\nSteps to reproduce the behavior:\n1. Go to '...'\n2. Click on '....'\n3. Scroll down to '....'\n4. See error\n\n**Expected behavior**\nA clear and concise description of what you expected to happen.\n\n**Screenshots**\nIf applicable, add screenshots to help explain your problem.\n\n**Desktop (please complete the following information):**\n - OS: [e.g. iOS]\n - Browser [e.g. chrome, safari]\n - Version [e.g. 22]\n\n**Smartphone (please complete the following information):**\n - Device: [e.g. iPhone6]\n - OS: [e.g. iOS8.1]\n - Browser [e.g. stock browser, safari]\n - Version [e.g. 22]\n\n**Additional context**\nAdd any other context about the problem here.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature_request.md",
    "content": "---\nname: Feature request\nabout: Suggest an idea for this project\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n\n**Is your feature request related to a problem? Please describe.**\nA clear and concise description of what the problem is. Ex. I'm always frustrated when [...]\n\n**Describe the solution you'd like**\nA clear and concise description of what you want to happen.\n\n**Describe alternatives you've considered**\nA clear and concise description of any alternative solutions or features you've considered.\n\n**Additional context**\nAdd any other context or screenshots about the feature request here.\n"
  },
  {
    "path": ".github/dependabot.yml",
    "content": "# To get started with Dependabot version updates, you'll need to specify which\n# package ecosystems to update and where the package manifests are located.\n# Please see the documentation for all configuration options:\n# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates\n\nversion: 2\nupdates:\n  - package-ecosystem: \"\" # See documentation for possible values\n    directory: \"/\" # Location of package manifests\n    schedule:\n      interval: \"daily\"\n"
  },
  {
    "path": ".github/workflows/codeql-analysis.yml",
    "content": "# For most projects, this workflow file will not need changing; you simply need\n# to commit it to your repository.\n#\n# You may wish to alter this file to override the set of languages analyzed,\n# or to provide custom queries or build logic.\n#\n# ******** NOTE ********\n# We have attempted to detect the languages in your repository. Please check\n# the `language` matrix defined below to confirm you have the correct set of\n# supported CodeQL languages.\n#\nname: \"CodeQL\"\n\non:\n  push:\n    branches: [ master ]\n  pull_request:\n    # The branches below must be a subset of the branches above\n    branches: [ master ]\n  schedule:\n    - cron: '27 12 * * 4'\n\njobs:\n  analyze:\n    name: Analyze\n    runs-on: ubuntu-latest\n    permissions:\n      actions: read\n      contents: read\n      security-events: write\n\n    strategy:\n      fail-fast: false\n      matrix:\n        language: [ 'python' ]\n        # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]\n        # Learn more:\n        # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed\n\n    steps:\n    - name: Checkout repository\n      uses: actions/checkout@v2\n\n    # Initializes the CodeQL tools for scanning.\n    - name: Initialize CodeQL\n      uses: github/codeql-action/init@v1\n      with:\n        languages: ${{ matrix.language }}\n        # If you wish to specify custom queries, you can do so here or in a config file.\n        # By default, queries listed here will override any specified in a config file.\n        # Prefix the list here with \"+\" to use these queries and those in the config file.\n        # queries: ./path/to/local/query, your-org/your-repo/queries@main\n\n    # Autobuild attempts to build any compiled languages  (C/C++, C#, or Java).\n    # If this step fails, then you should remove it and run the build manually (see below)\n    - name: Autobuild\n      uses: github/codeql-action/autobuild@v1\n\n    # ℹ️ Command-line programs to run using the OS shell.\n    # 📚 https://git.io/JvXDl\n\n    # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines\n    #    and modify them (or add more) to build your code if your project\n    #    uses a compiled language\n\n    #- run: |\n    #   make bootstrap\n    #   make release\n\n    - name: Perform CodeQL Analysis\n      uses: github/codeql-action/analyze@v1\n"
  },
  {
    "path": "01-classes/.gitkeep",
    "content": ""
  },
  {
    "path": "02-instances/.gitkeep",
    "content": ""
  },
  {
    "path": "03-class_attributes/06-class-attributes-1.py",
    "content": "#!/usr/bin/env python2.7\n\n# 06-class-attributes-1.py\n\n# Here we define an attribute under the class `YourClass`\n# as well as an attribute within the function.\n\n# The attribute defined in the class is called `class attributes`\n# and the attribute defined in the function is called `instance attributes`.\n\n\nclass YourClass(object):\n    classy = 10\n\n    def set_val(self):\n        self.insty = 100\n\n\ndd = YourClass()\ndd.classy  # This will fetch the class attribute 10.\ndd.set_val()\ndd.insty  # This will fetch the instance attribute 100.\n\n# Once `dd` is instantiated, we can access both the class and instance\n# attributes, ie.. dd.classy and dd.insty.\n"
  },
  {
    "path": "03-class_attributes/07-class-attributes-2.py",
    "content": "#!/usr/bin/env python\n\n# 07-class-attributes-2.py\n\n# The code below shows two important points:\n\n# a) A class attribute can be overridden in an instance, even\n# though it is bad due to breaking Encapsulation.\n\n# b) There is a lookup path for attributes in Python. The first being\n# the method defined within the class, and then the class above it.\n\n# We are overriding the 'classy' class attribute in the instance 'dd'.\n# When it's overridden, the python interpreter reads the overridden value.\n# But once the new value is deleted with 'del', the overridden value is no longer\n# present in the instance, and hence the lookup goes a level above and gets it from\n# the class.\n\n\nclass YourClass(object):\n    classy = \"class value\"\n\n\ndd = YourClass()\nprint(dd.classy)  # < This should return the string \"class value\"\n\ndd.classy = \"Instance value\"\nprint(dd.classy)  # This should return the string \"Instance value\"\n\n# This will delete the value set for 'dd.classy' in the instance.\ndel dd.classy\n# Since the overriding attribute was deleted, this will print 'class value'.\nprint(dd.classy)\n"
  },
  {
    "path": "03-class_attributes/08-class-instance-attributes-1.py",
    "content": "#!/usr/bin/env python\n\n# 08-class-instance-attributes-1.py\n\n# This code shows that an Instance can access it's own\n# attributes as well as Class attributes.\n\n# We have a class attribute named 'count', and we add 1 to\n# it each time we create an instance. This can help count the\n# number of instances at the time of instantiation.\n\n\nclass InstanceCounter(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = val\n        InstanceCounter.count += 1\n\n    def set_val(self, newval):\n        self.val = newval\n\n    def get_val(self):\n        print(self.val)\n\n    def get_count(self):\n        print(InstanceCounter.count)\n\n\na = InstanceCounter(5)\nb = InstanceCounter(10)\nc = InstanceCounter(15)\n\nfor obj in (a, b, c):\n    print(\"value of obj: %s\" % obj.get_val())\n    print(\"Count : %s\" % obj.get_count())\n"
  },
  {
    "path": "04-init_constructor/04-init_constructor-1.py",
    "content": "#!/usr/bin/env python\n\n# 04-init_constructor.py\n\n# __init__() is a constructor method which helps to\n# set initial values while instatiating a class.\n\n# __init__() will get called with the attributes set in __init__(),\n# when a class is instantiated.\n\n# The '__' before and after the method name denotes that\n# the method is private. It's called private or magic methods\n# since it's called internally and automatically.\n\n\nclass MyNum(object):\n    def __init__(self):\n        print(\"Calling the __init__() constructor!\\n\")\n        self.val = 0\n\n    def increment(self):\n        self.val = self.val + 1\n        print(self.val)\n\n\ndd = MyNum()\ndd.increment()  # will print 1\ndd.increment()  # will print 2\n"
  },
  {
    "path": "04-init_constructor/05-init_constructor-2.py",
    "content": "#!/usr/bin/env python\n\n# 05-init_constructor-2.py\n\n# We add a test in the __init__() constructor to check\n# if 'value' is an int or not.\n\n\nclass MyNum(object):\n    def __init__(self, value):\n        try:\n            value = int(value)\n        except ValueError:\n            value = 0\n        self.value = value\n\n    def increment(self):\n        self.value = self.value + 1\n        print(self.value)\n\n\na = MyNum(10)\na.increment()  # This should print 11\na.increment()  # This should print 12\n"
  },
  {
    "path": "05-encapsulation/01-encapsulation-1.py",
    "content": "#!/usr/bin/env python\n\n# encapsulation-1.py\n\n# Encapsulation means to preserve data in classes using methods\n# Here, we're setting the 'val' attribute through 'set_val()'.\n# See the next example, `encapsulation-2.py` for more info\n\n# In this example, we have two methods, `set_val` and `get_val`.\n# The first one sets the `val` value while the second one\n# prints/returns the value.\n\n\nclass MyClass(object):\n    def set_val(self, val):\n        self.value = val\n\n    def get_val(self):\n        # print(self.value)\n        return self.value\n\n\na = MyClass()\nb = MyClass()\n\na.set_val(10)\nb.set_val(100)\n\na.get_val()\nb.get_val()\n\n# NOTE: If you run this code, it won't print anything to the screen.\n# This is because, even if we're calling `a.get_val()` and `b.get_val()`,\n# the `get_val()` function doesn't contain a `print()` function.\n# If we want to get the output printed to screen, we should do any of\n# the following:\n\n# a) Either replace `return self.value` with `print(self.value)`\n# or add a print statement **above** `return` as `print(self.value)`.\n\n# b) Remove `return(self.value)` and replace it with `print(self.value)`\n"
  },
  {
    "path": "05-encapsulation/02-encapsulation-2.py",
    "content": "#!/usr/bin/env python\n\n# encapsulation-2.py\n\n# This example builds on top of `encapsulation-1.py`.\n# Here we see how we can set values in methods without\n# going through the method itself, ie.. how we can break\n# encapsulation.\n\n# NOTE: BREAKING ENCAPSULATION IS BAD.\n\n\nclass MyClass(object):\n    def set_val(self, val):\n        self.value = val\n\n    def get_val(self):\n        print(self.value)\n\n\na = MyClass()\nb = MyClass()\n\na.set_val(10)\nb.set_val(1000)\na.value = 100  # <== Overriding `set_value` directly\n# <== ie..  Breaking encapsulation\n\na.get_val()\nb.get_val()\n"
  },
  {
    "path": "05-encapsulation/03-encapsulation-3.py",
    "content": "#!/usr/bin/env python\n\n# 03-encapsulation-3.py\n\n# Here we look at another example, where we have three methods\n# set_val(), get_val(), and increment_val().\n\n# set_val() helps to set a value, get_val() prints the value,\n# and increment_val() increments the set value by 1.\n\n# We can still break encapsulation here by calling 'self.value'\n# directly in an instance, which is **BAD**.\n\n# set_val() forces us to input an integer, ie.. what the code wants\n# to work properly. Here, it's possible to break the encapsulation by\n# calling 'self.val` directly, which will cause unexpected results later.\n# In this example, the code is written to enforce an intger as input, if we\n# don't break encapsulation and go through the gateway 'set_val()'\n\n#\n\n\nclass MyInteger(object):\n    def set_val(self, val):\n        try:\n            val = int(val)\n        except ValueError:\n            return\n        self.val = val\n\n    def get_val(self):\n        print(self.val)\n\n    def increment_val(self):\n        self.val = self.val + 1\n        print(self.val)\n\n\na = MyInteger()\na.set_val(10)\na.get_val()\na.increment_val()\nprint(\"\\n\")\n\n# Trying to break encapsulation in a new instance with an int\nc = MyInteger()\nc.val = 15\nc.get_val()\nc.increment_val()\nprint(\"\\n\")\n\n# Trying to break encapsulation in a new instance with a str\nb = MyInteger()\nb.val = \"MyString\"  # <== Breaking encapsulation, works fine\nb.get_val()  # <== Prints the val set by breaking encap\nb.increment_val()  # This will fail, since str + int wont work\nprint(\"\\n\")\n"
  },
  {
    "path": "06-inheritance/09-inheritance-1.py",
    "content": "#!/usr/bin/env python\n\n# 09-inheritance-1.py\n\n# The code below shows how a class can inherit from another class.\n# We have two classes, `Date` and `Time`. Here `Time` inherits from\n# `Date`.\n\n# Any class inheriting from another class (also called a Parent class)\n# inherits the methods and attributes from the Parent class.\n\n# Hence, any instances created from the class `Time` can access\n# the methods defined in the parent class `Date`.\n\n\nclass Date(object):\n    def get_date(self):\n        print(\"2016-05-14\")\n\n\nclass Time(Date):\n    def get_time(self):\n        print(\"07:00:00\")\n\n\n# Creating an instance from `Date`\ndt = Date()\ndt.get_date()  # Accesing the `get_date()` method of `Date`\nprint(\"--------\")\n\n# Creating an instance from `Time`.\ntm = Time()\ntm.get_time()  # Accessing the `get_time()` method from `Time`.\n# Accessing the `get_date() which is defined in the parent class `Date`.\ntm.get_date()\n"
  },
  {
    "path": "06-inheritance/10-inheritance-2.py",
    "content": "#!/usr/bin/env python\n\n# 10-inheritance-2.py\n\n# The code below shows another example of inheritance\n# Dog and Cat are two classes which inherits from Animal.\n# This an instance created from Dog or Cat can access the methods\n# in the Animal class, ie.. eat().\n\n# The instance of 'Dog' can access the methods of the Dog class\n# and it's parent class 'Animal'.\n\n# The instance of 'Cat' can access the methods of the Cat class\n# and it's parent class 'Animal'.\n\n# But the instance created from 'Cat' cannot access the attributes\n# within the 'Dog' class, and vice versa.\n\n\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n\n    def eat(self, food):\n        print(\"%s is eating %s\" % (self.name, food))\n\n\nclass Dog(Animal):\n    def fetch(self, thing):\n        print(\"%s goes after the %s\" % (self.name, thing))\n\n\nclass Cat(Animal):\n    def swatstring(self):\n        print(\"%s shred the string!\" % self.name)\n\n\nd = Dog(\"Roger\")\nc = Cat(\"Fluffy\")\n\nd.fetch(\"paper\")\nd.eat(\"dog food\")\nprint(\"--------\")\nc.eat(\"cat food\")\nc.swatstring()\n\n# The below methods would fail, since the instances doesn't have\n# have access to the other class.\n\nc.fetch(\"frizbee\")\nd.swatstring()\n"
  },
  {
    "path": "06-inheritance/13-inheriting-init-constructor-1.py",
    "content": "#!/usr/bin/env python\n\n# 13-inheriting-init-constructor-1.py\n\n# This is a normal inheritance example from which we build\n# the next example. Make sure to read and understand the\n# next example '14-inheriting-init-constructor-2.py'.\n\n\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n\n\nclass Dog(Animal):\n    def fetch(self, thing):\n        print(\"%s goes after the %s\" % (self.name, thing))\n\n\nd = Dog(\"Roger\")\nprint(\"The dog's name is\", d.name)\nd.fetch(\"frizbee\")\n"
  },
  {
    "path": "06-inheritance/14-multiple-inheritance-1.py",
    "content": "#!/usr/bin/env python\n\n# 14-multiple-inheritance-1.py\n\n# Python supports multiple inheritance and uses a depth-first order\n# when searching for methods.\n# This search pattern is call MRO (Method Resolution Order)\n\n# This is the first example, which shows the lookup of a common\n# function named 'dothis()', which we'll continue in other examples.\n\n# As per the MRO output, it starts in class D, then B, A, and lastly C.\n\n# Both A and C contains 'dothis()'. Let's trace how the lookup happens.\n\n# As per the MRO output, it starts in class D, then B, A, and lastly C.\n\n# class `A` defines 'dothis()' and the search ends there. It doesn't go to C.\n\n# The MRO will show the full resolution path even if the full path is\n# not traversed.\n\n# The method lookup flow in this case is : D -> B -> A -> C\n\n\nclass A(object):\n    def dothis(self):\n        print(\"doing this in A\")\n\n\nclass B(A):\n    pass\n\n\nclass C(object):\n    def dothis(self):\n        print(\"doing this in C\")\n\n\nclass D(B, C):\n    pass\n\n\nd_instance = D()\nd_instance.dothis()  # <== This should print from class A.\n\nprint(\"\\nPrint the Method Resolution Order\")\nprint(D.mro())\n"
  },
  {
    "path": "06-inheritance/15-multiple-inheritance-2.py",
    "content": "#!/usr/bin/env python\n\n# 15-multiple-inheritance-2.py\n\n# Python supports multiple inheritance\n\n# It uses a depth-first order when searching for methods.\n# This search pattern is call MRO (Method Resolution Order)\n\n# This is a second example, which shows the lookup of 'dothis()'.\n# Both A and C contains 'dothis()'. Let's trace how the lookup happens.\n\n# As per the MRO output using depth-first search,\n# it starts in class D, then B, A, and lastly C.\n\n# Here we're looking for 'dothis()' which is defined in class `C`.\n# The lookup goes from D -> B -> A -> C.\n\n# Since class `A` doesn't have `dothis()`, the lookup goes back to class `C`\n# and finds it there.\n\n\nclass A(object):\n    def dothat(self):\n        print(\"Doing this in A\")\n\n\nclass B(A):\n    pass\n\n\nclass C(object):\n    def dothis(self):\n        print(\"\\nDoing this in C\")\n\n\nclass D(B, C):\n    \"\"\"Multiple Inheritance,\n    D inheriting from both B and C\"\"\"\n\n    pass\n\n\nd_instance = D()\n\nd_instance.dothis()\n\nprint(\"\\nPrint the Method Resolution Order\")\nprint(D.mro())\n"
  },
  {
    "path": "06-inheritance/16-multiple-inheritance-3.py",
    "content": "#!/usr/bin/env python\n\n# 16-multiple-inheritance-3.py\n\n# Python supports multiple inheritance\n# and uses a depth-first order when searching for methods.\n# This search pattern is call MRO (Method Resolution Order)\n\n# Example for \"Diamond Shape\" inheritance\n# Lookup can get complicated when multiple classes inherit\n# from multiple parent classes.\n\n# In order to avoid ambiguity while doing a lookup for a method\n# in various classes, from Python 2.3, the MRO lookup order has an\n# additional feature.\n\n# It still does a depth-first lookup, but if the occurrence of a class\n# happens multiple times in the MRO path, it removes the initial occurrence\n# and keeps the latter.\n\n# In the example below, class `D` inherits from `B` and `C`.\n# And both `B` and `C` inherits from `A`.\n# Both `A` and `C` has the method `dothis()`.\n\n# We instantiate `D` and requests the 'dothis()' method.\n# By default, the lookup should go D -> B -> A -> C -> A.\n# But from Python 2.3, in order to reduce the lookup time,\n# the MRO skips the classes which occur multiple times in the path.\n\n# Hence the lookup will be D -> B -> C -> A.\n\n\nclass A(object):\n    def dothis(self):\n        print(\"doing this in A\")\n\n\nclass B(A):\n    pass\n\n\nclass C(A):\n    def dothis(self):\n        print(\"doing this in C\")\n\n\nclass D(B, C):\n    pass\n\n\nd_instance = D()\nd_instance.dothis()\n\nprint(\"\\nPrint the Method Resolution Order\")\nprint(D.mro())\n"
  },
  {
    "path": "07-multiple_inheritance/.gitkeep",
    "content": ""
  },
  {
    "path": "08-mro/.gitkeep",
    "content": ""
  },
  {
    "path": "09-polymorphism/11-polymorphism-1.py",
    "content": "#!/usr/bin/env python\n\n# 11-polymorphism-1.py\n\n# Polymorphism means having the same interface/attributes in different\n# classes.\n\n# Polymorphism is the characteristic of being able to assign\n# a different meaning or usage in different contexts.\n# A not-so-clear/clean example is, different classes can have\n# the same function name.\n\n# Here, the class Dog and Cat has the same method named 'show_affection'\n# Even if they are same, both does different actions in the instance.\n#\n# Since the order of the lookup is\n# 'instance' -> 'class' -> 'parent class', even if the\n# 'class' and 'parent class' has functions with the same name,\n# the instance will only pick up the first hit,\n# ie.. from the 'class' and won't go to the parent class.\n\n\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n\n    def eat(self, food):\n        print(\"{0} eats {1}\".format(self.name, food))\n\n\nclass Dog(Animal):\n    def fetch(self, thing):\n        print(\"{0} goes after the {1}!\".format(self.name, thing))\n\n    def show_affection(self):\n        print(\"{0} wags tail\".format(self.name))\n\n\nclass Cat(Animal):\n    def swatstring(self):\n        print(\"{0} shreds more string\".format(self.name))\n\n    def show_affection(self):\n        print(\"{0} purrs\".format(self.name))\n\n\nfor a in (Dog(\"Rover\"), Cat(\"Fluffy\"), Cat(\"Lucky\"), Dog(\"Scout\")):\n    a.show_affection()\n"
  },
  {
    "path": "09-polymorphism/12-polymorphism-2.py",
    "content": "#!/usr/bin/env python\n\n# 12-polymorphism-2.py\n\n# Another example for Polymorphism are the several inbuilt\n# functions in Python. Take for example, the builtin function\n# called 'len'.\n\n# 'len' is available for almost all types, such as strings,\n# ints, floats, dictionaries, lists, tuples etc..\n# When len is called on a type, it actually calls the inbuilts\n# private function 'len' on that type or __len__\n\n# Every object type that supports 'len' will have a private\n# 'len' function inbuilt.\n\n# Hence, for example, a list type already has a 'len()'\n# function inbuilt in the Python code, and when you run the\n# len() function on the data type, it checks if the len\n# private function is available for that type or not.\n# If it is available, it runs that.\n\ntext = [\"Hello\", \"Hola\", \"helo\"]\nprint(len(text))\n\nprint(len(\"Hello\"))\nprint(len({\"a\": 1, \"b\": 2, \"c\": 3}))\n"
  },
  {
    "path": "10-instance_methods/17-instance_methods-1.py",
    "content": "#!/usr/bin/env python\n\n# 17-instance_methods-1.py\n\n# Instance methods are also known as Bound methods since the methods\n# within a class are bound to the instance created from the class, via\n# 'self'.\n\n\nclass A(object):\n    def method(*argv):\n        return argv\n\n\na = A()\nprint(a.method)\n\n# The print() function will print the following :\n#  python 17-instance_methods-1.py\n# <bound method A.method of <__main__.A object at 0x7fc91d83e790>>\n\n# The output shows that 'method' is a bound method\n"
  },
  {
    "path": "10-instance_methods/18-instance_methods-2.py",
    "content": "#!/usr/bin/env python3\n\n# 18-instance_methods.py\n\n# Instance methods are the normal way of accessing methods, seen in all\n# classes till now. ie.. by instantiating instances from a class, and\n# access the methods within the class. The usage of `self` is very\n# important in instance methods due to `self` being a hook/handle to the\n# instance itself, or the instance itself.\n\n# We look into a previous example, once more, to understand `Instance methods`.\n\n# We have an __init__() constructor, and three methods within the\n# `InstanceCounter` class.\n\n# Three instances a, b, and c are created from the class `InstanceCounter`.\n\n# Since the methods defined in the class are accessed through the\n# instances 'a', 'b', and 'c', these methods are called 'Instance\n# methods'.\n\n# Since the instance is bound to the methods defined in the class by the\n# keyword `self`, we also call `Instance methods` as 'Bound methods'.\n\n# In the code below, the instance is `obj` (the iterator) and we access\n# each method as `obj.set_val()`, `obj.get_val()`, and `obj.get_count`.\n\n\nclass InstanceCounter(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = val\n        InstanceCounter.count += 1\n\n    def set_val(self, newval):\n        self.val = newval\n\n    def get_val(self):\n        return self.val\n\n    def get_count(self):\n        return InstanceCounter.count\n\n\na = InstanceCounter(5)\nb = InstanceCounter(10)\nc = InstanceCounter(15)\n\nfor obj in (a, b, c):\n    print(\"Value of object: %s\" % (obj.get_val))\n    print(\"Count : %s \" % (obj.get_count))\n"
  },
  {
    "path": "11-class_methods/27-classmethod-1.py",
    "content": "#!/usr/bin/env python3\n\n# 19-class_methods-1.py\n\n# A classmethod is an inbuilt decorator which is called on functions via\n# @classmethod.\n\n# The @classmethod decorator marks the function/method as bound to the\n# class and not to an instance.\n\n# Remember that we used 'self' in a function within a class, which denoted\n# the instance. In class methods, we use `cls` which denotes the class\n# rather than the instance.\n\n# The following example is a very simple explanation of class-methods.\n\n# class_1() is a class method while class_2() is an instance method.\n\n# Class methods can be accessed by the class as well as the instance.\n\n# Instance methods can only be accessed by the Instance. That's why in this example, MyClass.class_2() will fail with an error.\n\n# NOTE : For the class MyClass:\n# \tMyClass is the class itself\n# \tMyClass() is an instance\n\n\nclass MyClass(object):\n    @classmethod\n    def class_1(cls):\n        print(\"Class method 1\")\n\n    def class_2(self):\n        print(\"Self/Instance method 1\")\n\n\nprint(\"Calling the class `MyClass` directly without an instance:\")\nMyClass.class_1()\n# MyClass.class_2()\n\n# NOTE: You will want to comment `MyClass.class_2()` once you hit the `TypeError`\n# to continue with the examples below.\n\nprint(\"\\nCalling the instance `MyClass()`:\")\nMyClass().class_1()\nMyClass().class_2()\n"
  },
  {
    "path": "11-class_methods/28-classmethod-2.py",
    "content": "#!/usr/bin/env python\n\n# 28-classmethod-2.py\n\n# Reference: https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/\n\n# Classmethods are decorators which are inbuilt in Python.\n# We decorate a function as a classemethod using the decorator\n# @classmethod.\n\n# Class methods are used for functions which need not be\n# called via an instance. Certain use cases may be:\n\n# a) Creating instances take resources, hence the methods/functions\n# which need necessarily\n\n\nclass MyClass(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = val\n        MyClass.count += 1\n\n    def set_val(self, newval):\n        self.val = newval\n\n    def get_val(self):\n        return self.val\n\n    @classmethod\n    def get_count(cls):\n        return cls.count\n\n\nobject_1 = MyClass(10)\nprint(\"\\nValue of object : %s\" % object_1.get_val())\nprint(MyClass.get_count())\n\nobject_2 = MyClass(20)\nprint(\"\\nValue of object : %s\" % object_2.get_val())\nprint(MyClass.get_count())\n\nobject_3 = MyClass(40)\nprint(\"\\nValue of object : %s\" % object_3.get_val())\nprint(MyClass.get_count())\n"
  },
  {
    "path": "12-static_methods/29-staticmethod-1.py",
    "content": "#!/usr/bin/env python3\n\n# 29-staticmethod-1.py\n\n\"\"\"\n# Refer https://arvimal.wordpress.com/2016/06/12/instance-class-static-methods-object-oriented-programming/\n# a) Static methods are functions methods which doesn’t need a binding \n# to a class or an instance.\n# b) Static methods, as well as Class methods, don’t require an instance \n# to be called.\n# c) Static methods doesn’t need  self or cls as the first argument \n# since it’s not bound to an instance or class.\n# d) Static methods are normal functions, but within a class.\n# e) Static methods are defined with the keyword @staticmethod \n# above the function/method.\n# f) Static methods are usually used to work on Class Attributes.\n\"\"\"\n\n\nclass MyClass(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = self.filterint(val)\n        MyClass.count += 1\n\n    @staticmethod\n    def filterint(value):\n        if not isinstance(value, int):\n            print(\"Entered value is not an INT, value set to 0\")\n            return 0\n        else:\n            return value\n\n\na = MyClass(5)\nb = MyClass(10)\nc = MyClass(15)\n\nprint(a.val)\nprint(b.val)\nprint(c.val)\nprint(a.filterint(100))\n"
  },
  {
    "path": "12-static_methods/30-staticmethod-2.py",
    "content": "#!/usr/bin/env python\nfrom __future__ import print_function\n\n# 30-staticmethod-2.py\n\n# Refer\n# https://arvimal.wordpress.com/2016/06/12/instance-class-static-methods-object-oriented-programming/\n\n# Static methods are functions/methods which doesn’t need a binding to a class or an instance.\n# Static methods, as well as Class methods, don’t require an instance to be called.\n# Static methods doesn’t need  self or cls as the first argument since it’s not bound to an instance or class.\n# Static methods are normal functions, but within a class.\n# Static methods are defined with the keyword @staticmethod above the function/method.\n# Static methods are usually used to work on Class Attributes.\n\n\nclass MyClass(object):\n    # A class attribute\n    count = 0\n\n    def __init__(self, name):\n        print(\"An instance is created!\")\n        self.name = name\n        MyClass.count += 1\n\n    # Our class method\n    @staticmethod\n    def status():\n        print(\"The total number of instances are \", MyClass.count)\n\n\nprint(MyClass.count)\n\nmy_func_1 = MyClass(\"MyClass 1\")\nmy_func_2 = MyClass(\"MyClass 2\")\nmy_func_3 = MyClass(\"MyClass 3\")\n\nMyClass.status()\nprint(MyClass.count)\n"
  },
  {
    "path": "13-decorators/19-decorators-1.py",
    "content": "#!/usr/bin/env python\n\n# 19-decorators-1.py\n# Decorators, as simple as it gets :)\n\n# Reference: Decorators 101 - A Gentle Introduction to Functional Programming.\n# By Jillian Munson - PyGotham 2014.\n# https://www.youtube.com/watch?v=yW0cK3IxlHc\n\n# Decorators are functions that compliment other functions,\n# or in other words, modify a function or method.\n\n# In the example below, we have a function named `decorated`.\n# This function just prints \"This happened\".\n# We have a decorator created named `inner_decorator()`.\n# This decorator function has an function within, which\n# does some operations (print stuff for simplicity) and then\n# returns the return-value of the internal function.\n\n# How does it work?\n# a) The function `decorated()` gets called.\n# b) Since the decorator `@my_decorator` is defined above\n# `decorated()`, `my_decorator()` gets called.\n# c) my_decorator() takes a function name as args, and hence `decorated()`\n# gets passed as the arg.\n# d) `my_decorator()` does it's job, and when it reaches `myfunction()`\n# calls the actual function, ie.. decorated()\n# e) Once the function `decorated()` is done, it gets back to `my_decorator()`.\n# f) Hence, using a decorator can drastically change the behavior of the\n# function you're actually executing.\n\n\ndef my_decorator(my_function):  # <-- (4)\n    def inner_decorator():  # <-- (5)\n        print(\"This happened before!\")  # <-- (6)\n        my_function()  # <-- (7)\n        print(\"This happens after \")  # <-- (10)\n        print(\"This happened at the end!\")  # <-- (11)\n\n    return inner_decorator\n    # return None\n\n\n@my_decorator  # <-- (3)\ndef my_decorated():  # <-- (2) <-- (8)\n    print(\"This happened!\")  # <-- (9)\n\n\nif __name__ == \"__main__\":\n    my_decorated()  # <-- (1)\n\n# This prints:\n# # python 19-decorators-1.py\n# This happened before!\n# This happened!\n# This happens after\n# This happened at the end!\n"
  },
  {
    "path": "13-decorators/20-decorators-2.py",
    "content": "#!/usr/bin/env python\n\n# Reference: Decorators 101 - A Gentle Introduction to Functional Programming.\n# By Jillian Munson - PyGotham 2014.\n# https://www.youtube.com/watch?v=yW0cK3IxlHc\n\n# 20-decorators-2.py\n# An updated version of 19-decorators-1.py\n\n# This code snippet takes the previous example, and add a bit more information\n# to the output.\n\nimport datetime\n\n\ndef my_decorator(inner):\n    def inner_decorator():\n        print(datetime.datetime.utcnow())\n        inner()\n        print(datetime.datetime.utcnow())\n\n    return inner_decorator\n\n\n@my_decorator\ndef decorated():\n    print(\"This happened!\")\n\n\nif __name__ == \"__main__\":\n    decorated()\n\n# This will print: (NOTE: The time will change of course :P)\n# # python 20-decorators-2.py\n# 2016-05-29 11:46:07.444330\n# This happened!\n# 2016-05-29 11:46:07.444367\n"
  },
  {
    "path": "13-decorators/21-decorators-3.py",
    "content": "#!/usr/bin/env python\n\n# Reference: Decorators 101 - A Gentle Introduction to Functional Programming.\n# By Jillian Munson - PyGotham 2014.\n# https://www.youtube.com/watch?v=yW0cK3IxlHc\n\n# This is an updated version of 20-decorators-2.py.\n# Here, the `decorated()` function takes an argument\n# and prints it back on terminal.\n\n# When the decorator `@my_decorator` is called, it\n# takes the function `decorated()` as its argument, and\n# the argument of `decorated()` as the argument of `inner_decorator()`.\n# Hence the arg `number` is passed to `num_copy`.\n\nimport datetime\n\n\ndef my_decorator(inner):\n    def inner_decorator(num_copy):\n        print(datetime.datetime.utcnow())\n        inner(int(num_copy) + 1)\n        print(datetime.datetime.utcnow())\n\n    return inner_decorator\n\n\n@my_decorator\ndef decorated(number):\n    print(\"This happened : \" + str(number))\n\n\nif __name__ == \"__main__\":\n    decorated(5)\n\n# This prints:\n# python 21-decorators-3.py\n# 2016-05-29 12:11:57.212125\n# This happened : 6\n# 2016-05-29 12:11:57.212168\n"
  },
  {
    "path": "13-decorators/22-decorators-4.py",
    "content": "#!/usr/bin/env python\n\n# Reference: Decorators 101 - A Gentle Introduction to Functional Programming.\n# By Jillian Munson - PyGotham 2014.\n# https://www.youtube.com/watch?v=yW0cK3IxlHc\n\n# 22-decorators-4.py\n\n# This example builds on the previous decorator examples.\n# The previous example, 21-decorators-3.py showed how to\n# deal with one argument passed to the function.\n\n# This example shows how we can deal with multiple args.\n\n# Reminder : `args` is a list of arguments passed, while\n# kwargs is a dictionary passed as arguments.\n\n\ndef decorator(inner):\n    def inner_decorator(*args, **kwargs):\n        print(args, kwargs)\n\n    return inner_decorator\n\n\n@decorator\ndef decorated(string_args):\n    print(\"This happened : \" + string_args)\n\n\nif __name__ == \"__main__\":\n    decorated(\"Hello, how are you?\")\n\n# This prints :\n# # python 22-decorators-4.py\n# ('Hello, how are you?',)\n"
  },
  {
    "path": "13-decorators/23-decorators-5.py",
    "content": "#!/usr/bin/env python\n\n# 23-decorators-5.py\n\n# Reference : https://www.youtube.com/watch?v=bxhuLgybIro\n\nfrom __future__ import print_function\n\n\n# 2. Decorator function\ndef handle_exceptions(func_name):\n    def inner(*args, **kwargs):\n        try:\n            return func_name(*args, **kwargs)\n        except Exception:\n            print(\"An exception was thrown : \", Exception)\n\n    return inner\n\n\n# 1. Main function\n@handle_exceptions\ndef divide(x, y):\n    return x / y\n\n\nprint(divide(8, 0))\n"
  },
  {
    "path": "13-decorators/24-decorators-6.py",
    "content": "#!/usr/bin/env python\n\n\ndef decorator(inner):\n    def inner_decorator(*args, **kwargs):\n        print(\"This function takes \" + str(len(args)) + \" arguments\")\n        inner(*args)\n\n    return inner_decorator\n\n\n@decorator\ndef decorated(string_args):\n    print(\"This happened: \" + str(string_args))\n\n\n@decorator\ndef alsoDecorated(num1, num2):\n    print(\"Sum of \" + str(num1) + \"and\" + str(num2) + \": \" + str(num1 + num2))\n\n\nif __name__ == \"__main__\":\n    decorated(\"Hello\")\n    alsoDecorated(1, 2)\n"
  },
  {
    "path": "13-decorators/25-decorators-7.py",
    "content": "#!/usr/bin/env python\n\n# 25-decorators-7.py\n\n# Reference https://www.youtube.com/watch?v=Slf1b3yUocc\n\n# We have two functions, one which adds two numbers,\n# and another which subtracts two numbers.\n\n# We apply the decorator @double which takes in the\n# functions that is called with the decorator, and doubles\n# the output of the respective function.\n\n\ndef double(my_func):\n    def inner_func(a, b):\n        return 2 * my_func(a, b)\n\n    return inner_func\n\n\n@double\ndef adder(a, b):\n    return a + b\n\n\n@double\ndef subtractor(a, b):\n    return a - b\n\n\nprint(adder(10, 20))\nprint(subtractor(6, 1))\n"
  },
  {
    "path": "13-decorators/26-class-decorators.py",
    "content": "#!/usr/bin/env python\n\n# 26-class-decorators.py\n\n# Reference : https://www.youtube.com/watch?v=Slf1b3yUocc\n# Talk by Mike Burns\n\n# Till the previous examples, we saw function decorators.\n# But decorators can be applied to Classes as well.\n# This example deals with class decorators.\n\n# NOTE: If you are creating a decorator for a class, you'll it\n# to return a Class.\n\n# NOTE: Similarly, if you are creating a decorator for a function,\n# you'll need it to return a function.\n\n\ndef honirific(cls):\n    class HonirificCls(cls):\n        def full_name(self):\n            return \"Dr. \" + super(HonirificCls, self).full_name()\n\n    return HonirificCls\n\n\n@honirific\nclass Name(object):\n    def __init__(self, first_name, last_name):\n        self.first_name = first_name\n        self.last_name = last_name\n\n    def full_name(self):\n        return \" \".join([self.first_name, self.last_name])\n\n\nresult = Name(\"Vimal\", \"A.R\").full_name()\nprint(\"Full name: {0}\".format(result))\n\n\n# This needs further check. Erroring out.\n"
  },
  {
    "path": "14-magic_methods/31-magicmethods-1.py",
    "content": "#!/usr/bin/env python\n\n# 30-magicmethods-1.py\n\n# In the backend, python is mostly objects and method\n# calls on objects.\n\n# Here we see an example, where the `print()` function\n# is just a call to the magic method `__repr__()`.\n\n\nclass PrintList(object):\n    def __init__(self, my_list):\n        self.mylist = my_list\n\n    def __repr__(self):\n        return str(self.mylist)\n\n\nprintlist = PrintList([\"a\", \"b\", \"c\"])\nprint(printlist.__repr__())\n"
  },
  {
    "path": "14-magic_methods/32-magicmethods-2.py",
    "content": "#!/usr/bin/env python\n\n# 31-magicmethods-2.py\n\n# In the backend, python is mostly objects and method\n# calls on objects.\n\n# To read more on magic methods, refer :\n# http://www.rafekettler.com/magicmethods.html\n\nmy_list_1 = [\"a\", \"b\", \"c\"]\n\nmy_list_2 = [\"d\", \"e\", \"f\"]\n\nprint(\"\\nCalling the `+` builtin with both lists\")\nprint(my_list_1 + my_list_2)\n\nprint(\"\\nCalling `__add__()` with both lists\")\nprint(my_list_1.__add__(my_list_2))\n"
  },
  {
    "path": "15-abstract_base_classes/34-abstractclasses-1.py",
    "content": "#!/usr/bin/env python\n\n# 34-abstractclasses-1.py\n\n# This code snippet talks about Abstract Base Classes (abc).\n\n# The `abc` module provides features to create\n# Abstract Base Classes.\n\n# To create an Abstract Base Class, set the `__metaclass__` magic method\n# to `abc.ABCMeta`. This will mark the respective class as an Abstract\n# Base Class.\n\n# Now, in order to specify the methods which are to be enforced on the\n# child classes, ie.. to create Abstract Methods, we use the decorator\n# @abc.abstractmethod on the methods we need.\n\n# The child class which inherits from an Abstract Base Class can implement\n# methods of their own, but *should always* implement the methods defined in\n# the parent ABC Class.\n\nimport abc\n\n\nclass My_ABC_Class(object):\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def set_val(self, val):\n        return\n\n    @abc.abstractmethod\n    def get_val(self):\n        return\n\n\n# Abstract Base Class defined above ^^^\n\n# Custom class inheriting from the above Abstract Base Class, below\n\n\nclass MyClass(My_ABC_Class):\n    def set_val(self, input):\n        self.val = input\n\n    def get_val(self):\n        print(\"\\nCalling the get_val() method\")\n        print(\"I'm part of the Abstract Methods defined in My_ABC_Class()\")\n        return self.val\n\n    def hello(self):\n        print(\"\\nCalling the hello() method\")\n        print(\"I'm *not* part of the Abstract Methods defined in My_ABC_Class()\")\n\n\nmy_class = MyClass()\n\nmy_class.set_val(10)\nprint(my_class.get_val())\nmy_class.hello()\n"
  },
  {
    "path": "15-abstract_base_classes/35-abstractclasses-2.py",
    "content": "#!/usr/bin/env python\n\n# 34-abstractclasses-1.py\n\n# This code snippet talks about Abstract Base Classes (abc).\n\n# The `abc` module provides features to create\n# Abstract Base Classes.\n\n# To create an Abstract Base Class, set the `__metaclass__` magic method\n# to `abc.ABCMeta`. This will mark the respective class as an Abstract\n# Base Class.\n\n# Now, in order to specify the methods which are to be enforced on the\n# child classes, ie.. to create Abstract Methods, we use the decorator\n# @abc.abstractmethod on the methods we need.\n\n# The child class which inherits from an Abstract Base Class can implement\n# methods of their own, but *should always* implement the methods defined in\n# the parent ABC Class.\n\n# NOTE: This code will error out. This is an example on what\n# happens when a child class doesn't implement the abstract methods\n# defined in the Parent Class.\n\nimport abc\n\n\nclass My_ABC_Class(object):\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def set_val(self, val):\n        return\n\n    @abc.abstractmethod\n    def get_val(self):\n        return\n\n\n# Abstract Base Class defined above ^^^\n\n# Custom class inheriting from the above Abstract Base Class, below\n\n\nclass MyClass(My_ABC_Class):\n    def set_val(self, input):\n        self.val = input\n\n    def hello(self):\n        print(\"\\nCalling the hello() method\")\n        print(\"I'm *not* part of the Abstract Methods defined in My_ABC_Class()\")\n\n\nmy_class = MyClass()\n\nmy_class.set_val(10)\nprint(my_class.get_val())\nmy_class.hello()\n"
  },
  {
    "path": "15-abstract_base_classes/36-abstractclasses-3.py",
    "content": "#!/usr/bin/env python\n\n# 34-abstractclasses-1.py\n\n# This code snippet talks about Abstract Base Classes (abc).\n\n# The `abc` module provides features to create\n# Abstract Base Classes.\n\n# To create an Abstract Base Class, set the `__metaclass__` magic method\n# to `abc.ABCMeta`. This will mark the respective class as an Abstract\n# Base Class.\n\n# Now, in order to specify the methods which are to be enforced on the\n# child classes, ie.. to create Abstract Methods, we use the decorator\n# @abc.abstractmethod on the methods we need.\n\n# The child class which inherits from an Abstract Base Class can implement\n# methods of their own, but *should always* implement the methods defined in\n# the parent ABC Class.\n\n# NOTE: This code will error out. This is an example on what\n# happens when a child class doesn't implement the abstract methods\n# defined in the Parent Class.\n\nimport abc\n\n\nclass My_ABC_Class(object):\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def set_val(self, val):\n        return\n\n    @abc.abstractmethod\n    def get_val(self):\n        return\n\n\n# Abstract Base Class defined above ^^^\n\n# Custom class inheriting from the above Abstract Base Class, below\n\n\nclass MyClass(My_ABC_Class):\n    def set_val(self, input):\n        self.val = input\n\n    def hello(self):\n        print(\"\\nCalling the hello() method\")\n        print(\"I'm *not* part of the Abstract Methods defined in My_ABC_Class()\")\n\n\nmy_class = My_ABC_Class()\n\nmy_class.set_val(10)\nprint(my_class.get_val())\nmy_class.hello()\n"
  },
  {
    "path": "16-method_overloading/37-method-overloading-1.py",
    "content": "#!/usr/bin/env python\n\n# 37-method-overloading-1.py\n\n# Reference: O'Reilly Learning Path:\n# http://shop.oreilly.com/product/0636920040057.do\n# Chapter 24 : Method Overloading - Extending and Providing\n\n# This code is an example on how we can extend a method inherited by\n# a child class from the Parent class.\n\n# 1) We have defined `MyClass()` as an abstract class,\n# and it has three methods, my_set_val(), my_get_val(), and print_doc().\n# 2) MyChildClass() inherits from MyClass()\n# 3) MyChildClass() extends the parent's my_set_val() method\n# by it's own my_set_val() method. It checks for the input,\n# checks if it's an integer, and then calls the my_set_val()\n# method in the parent.\n\n# 4) The print_doc() method in the Parent is an abstract method\n# and hence should be implemented in the child class MyChildClass()\n\n\nimport abc\n\n\nclass MyClass(object):\n\n    __metaclass__ = abc.ABCMeta\n\n    def my_set_val(self, value):\n        self.value = value\n\n    def my_get_val(self):\n        return self.value\n\n    @abc.abstractmethod\n    def print_doc(self):\n        return\n\n\nclass MyChildClass(MyClass):\n    def my_set_val(self, value):\n        if not isinstance(value, int):\n            value = 0\n        super(MyChildClass, self).my_set_val(self)\n\n    def print_doc(self):\n        print(\"Documentation for MyChild Class\")\n\n\nmy_instance = MyChildClass()\nmy_instance.my_set_val(100)\nprint(my_instance.my_get_val())\nprint(my_instance.print_doc())\n"
  },
  {
    "path": "16-method_overloading/38-method-overloading-2.py",
    "content": "#!/usr/bin/env python\n\n# 37-method-overloading-1.py\n\n# Reference: O'Reilly Learning Path:\n# http://shop.oreilly.com/product/0636920040057.do\n# Chapter 24 : Method Overloading - Extending and Providing\n\nimport abc\n\n\nclass GetSetParent(object):\n\n    __metaclass__ = abc.ABCMeta\n\n    def __init__(self, value):\n        self.val = 0\n\n    def set_val(self, value):\n        self.val = value\n\n    def get_val(self):\n        return self.val\n\n    @abc.abstractmethod\n    def showdoc(self):\n        return\n\n\nclass GetSetList(GetSetParent):\n    def __init__(self, value=0):\n        self.vallist = [value]\n\n    def get_val(self):\n        return self.vallist[-1]\n\n    def get_vals(self):\n        return self.vallist\n\n    def set_val(self, value):\n        self.vallist.append(value)\n\n    def showdoc(self):\n        print(\n            \"GetSetList object, len {0}, store history of values set\".format(\n                len(self.vallist)\n            )\n        )\n"
  },
  {
    "path": "16-method_overloading/39-method-overloading-3.py",
    "content": "#!/usr/bin/env python\n\n# 39-method-overloading-3.py\n\n# We've seen that inherited methods can be overloaded.\n# This is possible using in-built functions as well.\n\n# Let's see how we can overload methods from the `list` module.\n\n\nclass MyList(list):\n    def __getitem__(self, index):\n        if index == 0:\n            raise IndexError\n        if index > 0:\n            index -= 1\n        return list.__getitem__(self, index)\n\n    def __setitem__(self, index, value):\n        if index == 0:\n            raise IndexError\n        if index > 0:\n            index -= 1\n        list.__setitem__(self, index, value)\n\n\nx = MyList([\"a\", \"b\", \"c\"])\nprint(x)\nprint(\"-\" * 10)\n\nx.append(\"d\")\nprint(x)\nprint(\"-\" * 10)\n\nx.__setitem__(4, \"e\")\nprint(x)\nprint(\"-\" * 10)\n\nprint(x[1])\nprint(x.__getitem__(1))\nprint(\"-\" * 10)\n\nprint(x[4])\nprint(x.__getitem__(4))\n"
  },
  {
    "path": "17-super/40-super-1.py",
    "content": "#!/usr/bin/env python\n\n# 40-super-1.py\n\n# This is an example on how super() works\n# in Inheritance.\n\n# For more step-by-step details, refer :\n# https://arvimal.wordpress.com/2016/07/01/inheritance-and-super-object-oriented-programming/\n\n\nclass MyClass(object):\n    def func(self):\n        print(\"I'm being called from the Parent class\")\n\n\nclass ChildClass(MyClass):\n    def func(self):\n        print(\"I'm actually being called from the Child class\")\n        print(\"But...\")\n        # Calling the `func()` method from the Parent class.\n        super(ChildClass, self).func()\n\n\nmy_instance_2 = ChildClass()\nmy_instance_2.func()\n"
  },
  {
    "path": "17-super/41-super-2.py",
    "content": "#!/usr/bin/env python\n\n# 41-super-2.py\n\n# For more information on how this works, refer:\n\n# https://arvimal.wordpress.com/2016/07/01/inheritance-and-super-object-oriented-programming/\n\n# https://arvimal.wordpress.com/2016/06/29/inheritance-and-method-overloading-object-oriented-programming/\n\nimport abc\n\n\nclass MyClass(object):\n\n    __metaclass__ = abc.ABCMeta\n\n    def my_set_val(self, value):\n        self.value = value\n\n    def my_get_val(self):\n        return self.value\n\n    @abc.abstractmethod\n    def print_doc(self):\n        return\n\n\nclass MyChildClass(MyClass):\n    def my_set_val(self, value):\n        if not isinstance(value, int):\n            value = 0\n        super(MyChildClass, self).my_set_val(self)\n\n    def print_doc(self):\n        print(\"Documentation for MyChild Class\")\n\n\nmy_instance = MyChildClass()\nmy_instance.my_set_val(100)\nprint(my_instance.my_get_val())\nmy_instance.print_doc()\n"
  },
  {
    "path": "17-super/42-super-3.py",
    "content": "#!/usr/bin/env python\n\n# 42-super-3.py\n\n# super() and __init__()\n\n# Refer\n# https://arvimal.wordpress.com/2016/07/01/inheritance-and-super-object-oriented-programming/\n\n# http://www.blog.pythonlibrary.org/2014/01/21/python-201-what-is-super/\n\n\nclass A(object):\n    def foo(self):\n        print(\"A\")\n\n\nclass B(A):\n    def foo(self):\n        print(\"B\")\n\n\nclass C(A):\n    def foo(self):\n        print(\"C\")\n        super(C, self).foo()\n\n\nclass D(B, C):\n    def foo(self):\n        print(\"D\")\n        super(D, self).foo()\n\n\nd = D()\nd.foo()\n"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Contributor Covenant Code of Conduct\n\n## Our Pledge\n\nWe as members, contributors, and leaders pledge to make participation in our\ncommunity a harassment-free experience for everyone, regardless of age, body\nsize, visible or invisible disability, ethnicity, sex characteristics, gender\nidentity and expression, level of experience, education, socio-economic status,\nnationality, personal appearance, race, religion, or sexual identity\nand orientation.\n\nWe pledge to act and interact in ways that contribute to an open, welcoming,\ndiverse, inclusive, and healthy community.\n\n## Our Standards\n\nExamples of behavior that contributes to a positive environment for our\ncommunity include:\n\n* Demonstrating empathy and kindness toward other people\n* Being respectful of differing opinions, viewpoints, and experiences\n* Giving and gracefully accepting constructive feedback\n* Accepting responsibility and apologizing to those affected by our mistakes,\n  and learning from the experience\n* Focusing on what is best not just for us as individuals, but for the\n  overall community\n\nExamples of unacceptable behavior include:\n\n* The use of sexualized language or imagery, and sexual attention or\n  advances of any kind\n* Trolling, insulting or derogatory comments, and personal or political attacks\n* Public or private harassment\n* Publishing others' private information, such as a physical or email\n  address, without their explicit permission\n* Other conduct which could reasonably be considered inappropriate in a\n  professional setting\n\n## Enforcement Responsibilities\n\nCommunity leaders are responsible for clarifying and enforcing our standards of\nacceptable behavior and will take appropriate and fair corrective action in\nresponse to any behavior that they deem inappropriate, threatening, offensive,\nor harmful.\n\nCommunity leaders have the right and responsibility to remove, edit, or reject\ncomments, commits, code, wiki edits, issues, and other contributions that are\nnot aligned to this Code of Conduct, and will communicate reasons for moderation\ndecisions when appropriate.\n\n## Scope\n\nThis Code of Conduct applies within all community spaces, and also applies when\nan individual is officially representing the community in public spaces.\nExamples of representing our community include using an official e-mail address,\nposting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event.\n\n## Enforcement\n\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported to the community leaders responsible for enforcement at\n.\nAll complaints will be reviewed and investigated promptly and fairly.\n\nAll community leaders are obligated to respect the privacy and security of the\nreporter of any incident.\n\n## Enforcement Guidelines\n\nCommunity leaders will follow these Community Impact Guidelines in determining\nthe consequences for any action they deem in violation of this Code of Conduct:\n\n### 1. Correction\n\n**Community Impact**: Use of inappropriate language or other behavior deemed\nunprofessional or unwelcome in the community.\n\n**Consequence**: A private, written warning from community leaders, providing\nclarity around the nature of the violation and an explanation of why the\nbehavior was inappropriate. A public apology may be requested.\n\n### 2. Warning\n\n**Community Impact**: A violation through a single incident or series\nof actions.\n\n**Consequence**: A warning with consequences for continued behavior. No\ninteraction with the people involved, including unsolicited interaction with\nthose enforcing the Code of Conduct, for a specified period of time. This\nincludes avoiding interactions in community spaces as well as external channels\nlike social media. Violating these terms may lead to a temporary or\npermanent ban.\n\n### 3. Temporary Ban\n\n**Community Impact**: A serious violation of community standards, including\nsustained inappropriate behavior.\n\n**Consequence**: A temporary ban from any sort of interaction or public\ncommunication with the community for a specified period of time. No public or\nprivate interaction with the people involved, including unsolicited interaction\nwith those enforcing the Code of Conduct, is allowed during this period.\nViolating these terms may lead to a permanent ban.\n\n### 4. Permanent Ban\n\n**Community Impact**: Demonstrating a pattern of violation of community\nstandards, including sustained inappropriate behavior,  harassment of an\nindividual, or aggression toward or disparagement of classes of individuals.\n\n**Consequence**: A permanent ban from any sort of public interaction within\nthe community.\n\n## Attribution\n\nThis Code of Conduct is adapted from the [Contributor Covenant][homepage],\nversion 2.0, available at\nhttps://www.contributor-covenant.org/version/2/0/code_of_conduct.html.\n\nCommunity Impact Guidelines were inspired by [Mozilla's code of conduct\nenforcement ladder](https://github.com/mozilla/diversity).\n\n[homepage]: https://www.contributor-covenant.org\n\nFor answers to common questions about this code of conduct, see the FAQ at\nhttps://www.contributor-covenant.org/faq. Translations are available at\nhttps://www.contributor-covenant.org/translations.\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2021 Vimal A R\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# Object Oriented Programming in Python\n\nA deep-dive study guide covering core OOP concepts in Python, with annotated code examples. Originally written as interview preparation notes; expanded to be a complete reference.\n\n---\n\n## Table of Contents\n\n01. [Classes](#01-classes)\n02. [Instances, Instance Methods, Instance Attributes](#02-instances-instance-methods-instance-attributes)\n03. [Class Attributes](#03-class-attributes)\n04. [The `__init__` Constructor](#04-the-__init__-constructor)\n05. [Encapsulation](#05-encapsulation)\n06. [Inheritance](#06-inheritance)\n07. [Multiple Inheritance and Method/Attribute Lookup](#07-multiple-inheritance-and-methodattribute-lookup)\n08. [Method Resolution Order (MRO)](#08-method-resolution-order-mro)\n09. [Polymorphism](#09-polymorphism)\n10. [Instance Methods (Bound Methods)](#10-instance-methods-bound-methods)\n11. [Class Methods](#11-class-methods)\n12. [Static Methods](#12-static-methods)\n13. [Decorators](#13-decorators)\n14. [Magic Methods (Dunder Methods)](#14-magic-methods-dunder-methods)\n15. [Abstract Base Classes](#15-abstract-base-classes)\n16. [Method Overloading](#16-method-overloading)\n17. [super()](#17-super)\n\n---\n\n## 01. Classes\n\nA **class** is the fundamental building block of Object Oriented Programming. Think of it as a blueprint or template that describes the structure and behavior of objects created from it.\n\n- A class defines **attributes** (data) and **methods** (behavior).\n- Creating a class doesn't allocate memory for data — it only defines what an object of that class will look like.\n- Everything in Python is an object, and every object belongs to some class.\n\n```python\nclass Dog(object):\n    pass\n```\n\n`object` is the root of Python's class hierarchy. In Python 3, all classes implicitly inherit from `object`, so `class Dog:` and `class Dog(object):` are equivalent. In Python 2, you must explicitly write `class Dog(object):` to get a **new-style class** (as opposed to old-style classes that don't inherit from `object`).\n\n**Key terminology:**\n\n| Term | Meaning |\n| --- | --- |\n| Class | The blueprint / template |\n| Instance | A concrete object created from a class |\n| Attribute | Data stored on a class or instance |\n| Method | A function defined inside a class |\n\n**Visualizing a class as a blueprint:**\n\n```text\n┌────────────────────────────────────────┐\n│  CLASS  Dog          ← the blueprint  │\n│  ──────────────────────────────────── │\n│  Attributes:  name, breed             │\n│  Methods:     bark()                  │\n│               fetch(thing)            │\n└──────────────┬─────────────────────────┘\n               │  Dog(\"Rover\")   Dog(\"Fido\")\n               │  instantiate ──────────────────────┐\n               ▼                                    ▼\n  ┌────────────────────────┐      ┌────────────────────────┐\n  │  INSTANCE  rover       │      │  INSTANCE  fido        │\n  │  ────────────────────  │      │  ────────────────────  │\n  │  name  = \"Rover\"       │      │  name  = \"Fido\"        │\n  │  breed = \"Labrador\"    │      │  breed = \"Poodle\"      │\n  └────────────────────────┘      └────────────────────────┘\n```\n\nEach instance is independent — `rover.name` and `fido.name` have different values and changing one never affects the other.\n\n---\n\n## 02. Instances, Instance Methods, Instance Attributes\n\n### Instances\n\nAn **instance** is a concrete object created from a class using the call syntax `ClassName()`. Each instance is independent — changes to one instance do not affect others.\n\n```python\nclass Dog(object):\n    def bark(self):\n        print(\"Woof!\")\n\nrover = Dog()   # rover is an instance of Dog\nfido  = Dog()   # fido  is a separate instance of Dog\n\nrover.bark()    # Woof!\nfido.bark()     # Woof!\n```\n\n### Instance Attributes\n\n**Instance attributes** are data that belong to a specific instance. They are created by assigning to `self.something` inside a method, most commonly inside `__init__`.\n\n```python\nclass Dog(object):\n    def __init__(self, name):\n        self.name = name   # instance attribute\n\nrover = Dog(\"Rover\")\nfido  = Dog(\"Fido\")\n\nprint(rover.name)   # Rover\nprint(fido.name)    # Fido  — completely independent\n```\n\n### Instance Methods\n\n**Instance methods** are ordinary methods that operate on an instance. They always receive `self` as their first argument, which is the handle to the calling instance. Any method you define in a class that takes `self` is an instance method.\n\n```python\nclass Dog(object):\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(f\"{self.name} says Woof!\")\n\nrover = Dog(\"Rover\")\nrover.bark()   # Rover says Woof!\n```\n\nWhen you write `rover.bark()`, Python automatically passes `rover` as the `self` argument. This is equivalent to `Dog.bark(rover)`.\n\n---\n\n## 03. Class Attributes\n\n**Class attributes** are defined directly on the class body, outside any method. They are shared across all instances of the class.\n\n```python\nclass YourClass(object):\n    classy = 10          # class attribute\n\n    def set_val(self):\n        self.insty = 100  # instance attribute (set inside a method)\n\ndd = YourClass()\nprint(dd.classy)   # 10  — fetched from the class\ndd.set_val()\nprint(dd.insty)    # 100 — fetched from the instance\n```\n\n### Attribute Lookup Order\n\nPython's attribute lookup follows this order: **instance → class → parent classes**.\n\n```mermaid\nflowchart TD\n    A[\"Access  obj.attr\"] --> B{\"In instance\\n__dict__?\"}\n    B -- Yes --> C[\"✅ Return instance value\"]\n    B -- No  --> D{\"In class\\n__dict__?\"}\n    D -- Yes --> E[\"✅ Return class value\"]\n    D -- No  --> F{\"In a parent\\nclass?\"}\n    F -- Yes --> G[\"✅ Return parent value\"]\n    F -- No  --> H[\"❌ AttributeError\"]\n```\n\nThis has an important consequence: if you set an attribute on an instance with the same name as a class attribute, the instance attribute shadows the class one:\n\n```python\nclass YourClass(object):\n    classy = \"class value\"\n\ndd = YourClass()\nprint(dd.classy)       # \"class value\"  — from the class\n\ndd.classy = \"Instance value\"\nprint(dd.classy)       # \"Instance value\" — from the instance (shadows class attr)\n\ndel dd.classy          # remove the instance-level shadow\nprint(dd.classy)       # \"class value\"  — falls back to the class\n```\n\n> Code example: [03-class_attributes/07-class-attributes-2.py](03-class_attributes/07-class-attributes-2.py)\n\n### Class Attributes as Shared State\n\nClass attributes are useful for tracking shared state, such as a count of all instances ever created:\n\n```python\nclass InstanceCounter(object):\n    count = 0   # shared across all instances\n\n    def __init__(self, val):\n        self.val = val\n        InstanceCounter.count += 1   # update the class attribute directly\n\na = InstanceCounter(5)\nb = InstanceCounter(10)\nc = InstanceCounter(15)\n\nprint(InstanceCounter.count)   # 3\n```\n\n> Code example: [03-class_attributes/08-class-instance-attributes-1.py](03-class_attributes/08-class-instance-attributes-1.py)\n\n**Important:** Mutating a mutable class attribute (like a list) through an instance mutates the shared copy. Reassigning it (`self.attr = new_value`) creates an instance-level copy instead.\n\n---\n\n## 04. The `__init__` Constructor\n\n`__init__` is a **magic method** (also called a dunder method — double underscore) that Python calls automatically when a new instance is created. It is the class constructor.\n\n```python\nclass MyNum(object):\n    def __init__(self):\n        print(\"Instance created!\")\n        self.val = 0    # set initial state\n\n    def increment(self):\n        self.val += 1\n        print(self.val)\n\ndd = MyNum()    # prints \"Instance created!\"\ndd.increment()  # 1\ndd.increment()  # 2\n```\n\n> Code example: [02-init_constructor/04-init_constructor-1.py](02-init_constructor/04-init_constructor-1.py)\n\n### `__init__` with Arguments\n\n`__init__` can accept arguments to configure each instance differently:\n\n```python\nclass MyNum(object):\n    def __init__(self, value):\n        try:\n            value = int(value)\n        except ValueError:\n            value = 0\n        self.value = value\n\n    def increment(self):\n        self.value += 1\n        print(self.value)\n\na = MyNum(10)\na.increment()   # 11\na.increment()   # 12\n```\n\n> Code example: [02-init_constructor/05-init_constructor-2.py](02-init_constructor/05-init_constructor-2.py)\n\n### `__init__` is not `__new__`\n\n`__init__` *initializes* an already-created object. The actual memory allocation happens in `__new__`, which runs before `__init__`. In practice you rarely override `__new__` unless implementing singletons or immutable types like subclasses of `int` or `str`.\n\n---\n\n## 05. Encapsulation\n\n**Encapsulation** is the principle of bundling data and the methods that operate on that data within a class, and restricting direct access to internal state. The idea is to interact with an object only through its public interface (methods), not by reaching into its internals.\n\n```text\n                  ╔══════════════════════════════════════════╗\n                  ║           BankAccount object             ║\n                  ║                                          ║\n                  ║   ┌──────────────────────────────────┐   ║\n                  ║   │    Private / Internal State      │   ║\n                  ║   │    __balance = 1000              │   ║\n                  ║   └──────────────────────────────────┘   ║\n                  ║                                          ║\n                  ║   Public Interface  (the only door in)   ║\n                  ║   ┌────────────┐  ┌───────────────────┐  ║\n                  ║   │ deposit()  │  │  get_balance()    │  ║\n                  ║   └─────▲──────┘  └────────▲──────────┘  ║\n                  ╚═════════╪═══════════════════╪════════════╝\n                            │                   │\n                     external code         external code\n                     calls deposit()      calls get_balance()\n\n  ✅  acct.deposit(50)          — goes through the interface\n  ❌  acct.__balance = 9999     — breaks encapsulation (bypasses validation)\n```\n\n### Basic Example\n\n```python\nclass MyClass(object):\n    def set_val(self, val):\n        self.value = val\n\n    def get_val(self):\n        return self.value\n\na = MyClass()\na.set_val(10)\nprint(a.get_val())   # 10\n```\n\n> Code example: [01-encapsulation/01-encapsulation-1.py](01-encapsulation/01-encapsulation-1.py)\n\n### Breaking Encapsulation\n\nPython does **not** enforce encapsulation at the language level. You can bypass a setter and write directly to the attribute:\n\n```python\na = MyClass()\na.set_val(10)\na.value = 999    # bypasses set_val() — breaking encapsulation\nprint(a.get_val())  # 999\n```\n\n> Code example: [01-encapsulation/02-encapsulation-2.py](01-encapsulation/02-encapsulation-2.py)\n\nThis is **bad practice** because the setter may contain validation logic. If you bypass it, you lose those guarantees:\n\n```python\nclass MyInteger(object):\n    def set_val(self, val):\n        try:\n            val = int(val)\n        except ValueError:\n            return\n        self.val = val\n\n    def get_val(self):\n        print(self.val)\n\n    def increment_val(self):\n        self.val += 1\n\nb = MyInteger()\nb.val = \"MyString\"   # breaking encapsulation — now val is a string\nb.get_val()          # prints \"MyString\"\nb.increment_val()    # TypeError: can only concatenate str (not \"int\") to str\n```\n\n> Code example: [01-encapsulation/03-encapsulation-3.py](01-encapsulation/03-encapsulation-3.py)\n\n### Name Mangling for Private Attributes\n\nPython uses **name mangling** as a convention for pseudo-private attributes:\n\n- `_name` — single underscore: \"internal use\" by convention, not enforced. Importing modules with `from module import *` will skip these.\n- `__name` — double underscore: Python renames this to `_ClassName__name` internally, making accidental access harder (but not impossible).\n\n```python\nclass BankAccount(object):\n    def __init__(self, balance):\n        self.__balance = balance   # name-mangled to _BankAccount__balance\n\n    def deposit(self, amount):\n        self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\nacct = BankAccount(100)\nacct.deposit(50)\nprint(acct.get_balance())      # 150\n# print(acct.__balance)        # AttributeError\nprint(acct._BankAccount__balance)  # 150 — still accessible, just harder\n```\n\n### Properties: The Pythonic Way\n\nThe `@property` decorator provides a clean way to add getter/setter logic without changing the external interface:\n\n```python\nclass Temperature(object):\n    def __init__(self, celsius):\n        self._celsius = celsius\n\n    @property\n    def celsius(self):\n        return self._celsius\n\n    @celsius.setter\n    def celsius(self, value):\n        if value < -273.15:\n            raise ValueError(\"Temperature below absolute zero!\")\n        self._celsius = value\n\n    @property\n    def fahrenheit(self):\n        return self._celsius * 9 / 5 + 32\n\nt = Temperature(100)\nprint(t.celsius)      # 100\nprint(t.fahrenheit)   # 212.0\nt.celsius = 0\nprint(t.fahrenheit)   # 32.0\n```\n\n---\n\n## 06. Inheritance\n\n**Inheritance** allows a class (the **child** or **subclass**) to acquire the attributes and methods of another class (the **parent** or **superclass**). This enables code reuse and the modeling of \"is-a\" relationships.\n\n```mermaid\nclassDiagram\n    direction TB\n    class Animal {\n        +name : str\n        +__init__(name)\n        +eat(food)\n    }\n    class Dog {\n        +fetch(thing)\n    }\n    class Cat {\n        +swatstring()\n    }\n\n    Animal <|-- Dog : inherits\n    Animal <|-- Cat : inherits\n\n    note for Dog \"Inherits: __init__, eat()\\nOwn:       fetch()\"\n    note for Cat \"Inherits: __init__, eat()\\nOwn:       swatstring()\"\n```\n\n```python\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n\n    def eat(self, food):\n        print(f\"{self.name} is eating {food}\")\n\n\nclass Dog(Animal):          # Dog inherits from Animal\n    def fetch(self, thing):\n        print(f\"{self.name} goes after the {thing}\")\n\n\nclass Cat(Animal):          # Cat also inherits from Animal\n    def swatstring(self):\n        print(f\"{self.name} shreds the string!\")\n\n\nd = Dog(\"Roger\")\nc = Cat(\"Fluffy\")\n\nd.fetch(\"paper\")    # Dog's own method\nd.eat(\"dog food\")   # inherited from Animal\n\nc.eat(\"cat food\")   # inherited from Animal\nc.swatstring()      # Cat's own method\n```\n\n> Code examples: [04-inheritance/09-inheritance-1.py](04-inheritance/09-inheritance-1.py), [04-inheritance/10-inheritance-2.py](04-inheritance/10-inheritance-2.py)\n\n**Key rules:**\n\n- A child class inherits **all** methods and attributes from the parent.\n- Sibling classes (`Dog` and `Cat`) cannot access each other's methods.\n- A child can **override** a parent method by defining one with the same name.\n- The parent class being inherited from is also called the **base class**.\n\n### Inheriting `__init__`\n\nIf a child class does not define its own `__init__`, it inherits the parent's:\n\n```python\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n\nclass Dog(Animal):\n    def fetch(self, thing):\n        print(f\"{self.name} goes after the {thing}\")\n\nd = Dog(\"Roger\")   # uses Animal's __init__\nprint(d.name)      # Roger\nd.fetch(\"frizbee\")\n```\n\n> Code example: [04-inheritance/13-inheriting-init-constructor-1.py](04-inheritance/13-inheriting-init-constructor-1.py)\n\nIf the child defines its own `__init__`, it completely replaces the parent's — unless you explicitly call the parent's `__init__` using `super()` (see [section 17](#17-super)).\n\n### Overriding Methods\n\nA child class can override any parent method:\n\n```python\nclass Animal(object):\n    def speak(self):\n        print(\"...\")\n\nclass Dog(Animal):\n    def speak(self):        # overrides Animal.speak\n        print(\"Woof!\")\n\nclass Cat(Animal):\n    def speak(self):        # overrides Animal.speak\n        print(\"Meow!\")\n\nDog().speak()   # Woof!\nCat().speak()   # Meow!\n```\n\n---\n\n## 07. Multiple Inheritance and Method/Attribute Lookup\n\nPython allows a class to inherit from **multiple parent classes** simultaneously.\n\n```mermaid\nclassDiagram\n    direction TB\n    class Flyable {\n        +fly()\n    }\n    class Swimmable {\n        +swim()\n    }\n    class Duck {\n        +fly()\n        +swim()\n    }\n    Flyable  <|-- Duck : inherits\n    Swimmable <|-- Duck : inherits\n```\n\n```python\nclass Flyable(object):\n    def fly(self):\n        print(\"Flying!\")\n\nclass Swimmable(object):\n    def swim(self):\n        print(\"Swimming!\")\n\nclass Duck(Flyable, Swimmable):\n    pass\n\nd = Duck()\nd.fly()    # inherited from Flyable\nd.swim()   # inherited from Swimmable\n```\n\n### Attribute Lookup Chain\n\nWhen you access `instance.attribute`, Python follows this chain:\n1. The instance's own `__dict__`\n2. The instance's class\n3. Parent classes in **MRO order** (see next section)\n\n> Code examples: [04-inheritance/14-multiple-inheritance-1.py](04-inheritance/14-multiple-inheritance-1.py), [04-inheritance/15-multiple-inheritance-2.py](04-inheritance/15-multiple-inheritance-2.py)\n\n---\n\n## 08. Method Resolution Order (MRO)\n\n**MRO** defines the order in which Python searches classes for a method or attribute. It determines which method wins when multiple classes in the hierarchy define the same name.\n\n### Viewing the MRO\n\n```python\nprint(ClassName.mro())\n# or\nprint(ClassName.__mro__)\n```\n\n### The C3 Linearization Algorithm\n\nPython 3 (and Python 2 new-style classes from 2.3 onwards) uses the **C3 linearization algorithm**, not a naive depth-first search. C3 guarantees:\n\n- The class itself comes first.\n- A class always appears before its parents.\n- The order in which parents are listed in the class definition is preserved.\n- No class appears more than once (duplicates are resolved by keeping the *last* occurrence).\n\n### Simple Inheritance — Depth-First\n\n```text\nD inherits from B and C.\nB inherits from A.\nBoth A and C define dothis().\n\nNaive depth-first path: D → B → A → C → A\nMRO (with C3):          D → B → A → C → object\n```\n\n```mermaid\ngraph TD\n    A[\"A  ✦ has dothis()\"]\n    B[\"B  (no dothis)\"]\n    C[\"C  ✦ has dothis()\"]\n    D[\"D  (start here)\"]\n\n    B --> A\n    D --> B\n    D --> C\n\n    style D fill:#4a90d9,color:#fff\n    style A fill:#27ae60,color:#fff\n    style C fill:#e67e22,color:#fff\n```\n\nMRO lookup path for `D().dothis()`:\n\n```mermaid\nflowchart LR\n    D[\"D\"] --> B[\"B\"] --> A[\"A ✦ FOUND\"] --> stop([\"stop\"])\n    style A fill:#27ae60,color:#fff\n    style D fill:#4a90d9,color:#fff\n    style stop fill:#ccc\n```\n\n```python\nclass A(object):\n    def dothis(self): print(\"doing this in A\")\n\nclass B(A): pass\n\nclass C(object):\n    def dothis(self): print(\"doing this in C\")\n\nclass D(B, C): pass\n\nd = D()\nd.dothis()       # \"doing this in A\"  — found in A before reaching C\nprint(D.mro())   # [D, B, A, C, object]\n```\n\n> Code example: [04-inheritance/14-multiple-inheritance-1.py](04-inheritance/14-multiple-inheritance-1.py)\n\n### Diamond Inheritance — C3 Removes Duplicates\n\nThe **diamond problem** arises when two parents both inherit from the same grandparent. Naive depth-first would visit the grandparent twice.\n\n```text\nD inherits from B and C.\nB inherits from A.\nC inherits from A.\nBoth A and C define dothis().\n\nNaive path:    D → B → A → C → A   (A appears twice)\nC3 MRO:        D → B → C → A       (A's early occurrence removed)\n```\n\nThe shape of this hierarchy gives it its name — the **diamond problem**:\n\n```mermaid\ngraph TD\n    A[\"A  ✦ has dothis()\"]\n    B[\"B  (no dothis)\"]\n    C[\"C  ✦ has dothis()\"]\n    D[\"D  (start here)\"]\n\n    B --> A\n    C --> A\n    D --> B\n    D --> C\n\n    style D fill:#4a90d9,color:#fff\n    style A fill:#27ae60,color:#fff\n    style C fill:#e67e22,color:#fff\n```\n\nC3 MRO pushes `A` to the end so each class appears exactly once:\n\n```mermaid\nflowchart LR\n    D[\"D\"] --> B[\"B\"] --> C[\"C ✦ FOUND\"] --> A[\"A\"] --> obj[\"object\"]\n    style C fill:#e67e22,color:#fff\n    style D fill:#4a90d9,color:#fff\n```\n\n`D().dothis()` resolves to **C**, not A — because C3 delayed A until after C.\n\n```python\nclass A(object):\n    def dothis(self): print(\"doing this in A\")\n\nclass B(A): pass\n\nclass C(A):\n    def dothis(self): print(\"doing this in C\")\n\nclass D(B, C): pass\n\nd = D()\nd.dothis()       # \"doing this in C\"  — C comes before A in the MRO\nprint(D.mro())   # [D, B, C, A, object]\n```\n\nBecause `A` appears in both `B`'s and `C`'s lineage, C3 pushes `A` to the end — after `C`. So the method is found in `C`, not `A`.\n\n> Code example: [04-inheritance/16-multiple-inheritance-3.py](04-inheritance/16-multiple-inheritance-3.py)\n\n**Interview tip:** When asked \"which method gets called?\", trace the MRO using `ClassName.mro()`. Never guess.\n\n---\n\n## 09. Polymorphism\n\n**Polymorphism** means \"many forms.\" In OOP, it means different classes can expose the same interface (method name), but each class implements it differently.\n\nThe key insight: **same call, different behavior depending on the actual type**.\n\n```mermaid\nflowchart LR\n    caller[\"for a in animals:\\n    a.show_affection()\"]\n\n    caller --> Dog[\"Dog instance\\n(Rover)\"]\n    caller --> Cat1[\"Cat instance\\n(Fluffy)\"]\n    caller --> Cat2[\"Cat instance\\n(Lucky)\"]\n    caller --> Dog2[\"Dog instance\\n(Scout)\"]\n\n    Dog  --> r1[\"'Rover wags tail'\"]\n    Cat1 --> r2[\"'Fluffy purrs'\"]\n    Cat2 --> r3[\"'Lucky purrs'\"]\n    Dog2 --> r4[\"'Scout wags tail'\"]\n\n    style Dog  fill:#5dade2,color:#fff\n    style Dog2 fill:#5dade2,color:#fff\n    style Cat1 fill:#58d68d,color:#fff\n    style Cat2 fill:#58d68d,color:#fff\n```\n\n### Polymorphism through Inheritance\n\n```python\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n\nclass Dog(Animal):\n    def show_affection(self):\n        print(f\"{self.name} wags tail\")\n\nclass Cat(Animal):\n    def show_affection(self):\n        print(f\"{self.name} purrs\")\n\nanimals = [Dog(\"Rover\"), Cat(\"Fluffy\"), Cat(\"Lucky\"), Dog(\"Scout\")]\nfor a in animals:\n    a.show_affection()   # each class handles it differently\n```\n\n> Code example: [05-polymorphism/11-polymorphism-1.py](05-polymorphism/11-polymorphism-1.py)\n\n### Duck Typing\n\nPython's polymorphism is rooted in **duck typing**: \"If it walks like a duck and quacks like a duck, it's a duck.\" Python doesn't care about the type of an object — it only cares whether the object has the method you're trying to call.\n\n```python\nclass Duck:\n    def speak(self): print(\"Quack!\")\n\nclass Person:\n    def speak(self): print(\"I'm speaking!\")\n\nfor obj in (Duck(), Person()):\n    obj.speak()   # works — both have speak()\n```\n\nNo common base class needed.\n\n### Built-in Polymorphism\n\nPython's built-in functions are themselves polymorphic. `len()` works on strings, lists, dicts, and any object that implements `__len__`:\n\n```python\nprint(len(\"Hello\"))             # 5\nprint(len([1, 2, 3]))           # 3\nprint(len({\"a\": 1, \"b\": 2}))   # 2\n```\n\nInternally, `len(x)` calls `x.__len__()`. Any class that defines `__len__` participates in this protocol.\n\n> Code example: [05-polymorphism/12-polymorphism-2.py](05-polymorphism/12-polymorphism-2.py)\n\n---\n\n## 10. Instance Methods (Bound Methods)\n\nInstance methods are the default method type in Python. They take `self` as their first parameter, giving them access to the calling instance.\n\nWhen you access an instance method via `instance.method`, Python returns a **bound method** — the function is bound to the instance, meaning `self` is automatically filled in.\n\n```python\nclass A(object):\n    def method(self):\n        return self\n\na = A()\nprint(a.method)\n# <bound method A.method of <__main__.A object at 0x...>>\n```\n\n> Code example: [07-static_class_instance_methods/17-instance_methods-1.py](07-static_class_instance_methods/17-instance_methods-1.py)\n\nCalling `a.method()` is exactly equivalent to calling `A.method(a)`. Python inserts `a` as the first argument automatically — this is the mechanism behind `self`.\n\n```python\nclass InstanceCounter(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = val\n        InstanceCounter.count += 1\n\n    def set_val(self, newval):\n        self.val = newval\n\n    def get_val(self):\n        return self.val\n\n    def get_count(self):\n        return InstanceCounter.count\n\na = InstanceCounter(5)\nb = InstanceCounter(10)\nc = InstanceCounter(15)\n\nfor obj in (a, b, c):\n    print(f\"Value: {obj.get_val()}, Count: {obj.get_count()}\")\n```\n\n> Code example: [07-static_class_instance_methods/18-instance_methods-2.py](07-static_class_instance_methods/18-instance_methods-2.py)\n\n---\n\n## 11. Class Methods\n\nA **class method** is decorated with `@classmethod`. Instead of receiving the instance (`self`) as the first argument, it receives the **class itself** (`cls`). This means it operates on the class rather than on a specific instance.\n\n```python\nclass MyClass(object):\n    @classmethod\n    def class_method(cls):\n        print(f\"Called on class: {cls}\")\n\n    def instance_method(self):\n        print(f\"Called on instance: {self}\")\n\nMyClass.class_method()     # works — called on the class directly\nMyClass().class_method()   # also works — cls is still MyClass\n# MyClass.instance_method()  # TypeError — no instance to bind self\nMyClass().instance_method() # works\n```\n\n> Code example: [07-static_class_instance_methods/27-classmethod-1.py](07-static_class_instance_methods/27-classmethod-1.py)\n\n### Common Use Case: Factory Methods and Class-Level State\n\n```python\nclass MyClass(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = val\n        MyClass.count += 1\n\n    def get_val(self):\n        return self.val\n\n    @classmethod\n    def get_count(cls):\n        return cls.count   # cls is MyClass here\n\nobj1 = MyClass(10)\nobj2 = MyClass(20)\n\nprint(MyClass.get_count())   # 2 — no instance needed\nprint(obj1.get_count())      # 2 — also accessible via instance\n```\n\n> Code example: [07-static_class_instance_methods/28-classmethod-2.py](07-static_class_instance_methods/28-classmethod-2.py)\n\n**Class methods as alternative constructors** is a common pattern:\n\n```python\nclass Date(object):\n    def __init__(self, year, month, day):\n        self.year, self.month, self.day = year, month, day\n\n    @classmethod\n    def from_string(cls, date_string):\n        year, month, day = map(int, date_string.split(\"-\"))\n        return cls(year, month, day)   # cls() calls __init__\n\nd = Date.from_string(\"2024-04-17\")\nprint(d.year, d.month, d.day)   # 2024 4 17\n```\n\n### Visual: What Each Method Type Can See\n\n```text\n ┌─────────────────────────────────────────────────────────────────────────┐\n │                          MyClass                                        │\n │                                                                         │\n │  class_attr = 0    ◄──────── shared class-level data                   │\n │                                                                         │\n │  def __init__(self):                                                    │\n │      self.inst_attr = 10   ◄── per-instance data                       │\n │                                                                         │\n │  ┌──────────────────┐  ┌──────────────────┐  ┌──────────────────────┐  │\n │  │  Instance Method │  │  Class Method    │  │  Static Method       │  │\n │  │  def m(self):    │  │  @classmethod    │  │  @staticmethod       │  │\n │  │                  │  │  def m(cls):     │  │  def m():            │  │\n │  │  ✅ inst_attr    │  │  ❌ inst_attr    │  │  ❌ inst_attr        │  │\n │  │  ✅ class_attr   │  │  ✅ class_attr   │  │  ⚠️  class_attr      │  │\n │  │     via self     │  │     via cls      │  │     only via         │  │\n │  │                  │  │                  │  │     MyClass.attr     │  │\n │  └──────────────────┘  └──────────────────┘  └──────────────────────┘  │\n │  Called via instance    Via class OR instance  Via class OR instance    │\n └─────────────────────────────────────────────────────────────────────────┘\n```\n\n### Comparison: instance method vs class method vs static method\n\n| | Instance Method | Class Method | Static Method |\n| --- | --- | --- | --- |\n| First arg | `self` (instance) | `cls` (class) | none |\n| Access instance state? | yes | no | no |\n| Access class state? | yes (via `self.__class__`) | yes (via `cls`) | only explicitly |\n| Called on class directly? | no | yes | yes |\n| Decorator | none | `@classmethod` | `@staticmethod` |\n\n---\n\n## 12. Static Methods\n\nA **static method** is decorated with `@staticmethod`. It receives neither `self` nor `cls`. It is essentially a regular function that lives inside a class for organizational purposes.\n\nUse a static method when the function is logically related to the class but does not need to access or modify class or instance state.\n\n```python\nclass MyClass(object):\n    count = 0\n\n    def __init__(self, val):\n        self.val = self.filterint(val)   # calling static method from __init__\n        MyClass.count += 1\n\n    @staticmethod\n    def filterint(value):\n        if not isinstance(value, int):\n            print(\"Value is not an int, defaulting to 0\")\n            return 0\n        return value\n\na = MyClass(5)\nb = MyClass(\"hello\")   # Value is not an int, defaulting to 0\nprint(a.val)           # 5\nprint(b.val)           # 0\nprint(MyClass.filterint(42))   # callable on the class directly\n```\n\n> Code example: [07-static_class_instance_methods/29-staticmethod-1.py](07-static_class_instance_methods/29-staticmethod-1.py)\n\n```python\nclass MyClass(object):\n    count = 0\n\n    def __init__(self, name):\n        self.name = name\n        MyClass.count += 1\n\n    @staticmethod\n    def status():\n        print(f\"Total instances: {MyClass.count}\")\n\nMyClass(\"Alpha\")\nMyClass(\"Beta\")\nMyClass(\"Gamma\")\n\nMyClass.status()   # Total instances: 3\n```\n\n> Code example: [07-static_class_instance_methods/30-staticmethod-2.py](07-static_class_instance_methods/30-staticmethod-2.py)\n\n**When to use which:**\n\n- Use **instance method** when the method needs to read or write instance state.\n- Use **class method** when the method operates on class-level state or serves as an alternative constructor.\n- Use **static method** when the method is a utility that doesn't need access to either.\n\n---\n\n## 13. Decorators\n\nA **decorator** is a function that takes another function (or class) as input, wraps it with additional behavior, and returns the modified function. The `@decorator_name` syntax is syntactic sugar for:\n\n```python\nmy_function = decorator(my_function)\n```\n\n**What a decorator actually does — before and after:**\n\n```text\nBEFORE decoration                AFTER  @my_decorator\n─────────────────                ────────────────────────────────────────\n                                 ┌──────────────────────────────────────┐\n                                 │  inner_decorator()   ← what you call │\n                                 │  ┌────────────────────────────────┐  │\n                                 │  │  print(\"Before the function\")  │  │\n┌─────────────────────┐          │  ├────────────────────────────────┤  │\n│  my_decorated()     │  ──────► │  │  my_decorated()  ← original   │  │\n│                     │          │  │  print(\"This happened!\")       │  │\n│  print(\"This        │          │  ├────────────────────────────────┤  │\n│         happened!\") │          │  │  print(\"After the function\")   │  │\n└─────────────────────┘          │  └────────────────────────────────┘  │\n                                 └──────────────────────────────────────┘\n```\n\nThe original function is **wrapped** — it still runs, but now surrounded by the decorator's extra behavior.\n\n### Anatomy of a Decorator\n\n```python\ndef my_decorator(my_function):        # (1) receives the function to wrap\n    def inner_decorator():            # (2) defines the wrapper\n        print(\"Before the function\")  # (3) runs before\n        my_function()                 # (4) calls the original\n        print(\"After the function\")   # (5) runs after\n    return inner_decorator            # (6) returns the wrapper, not the result\n\n@my_decorator\ndef my_decorated():\n    print(\"This happened!\")\n\nmy_decorated()\n# Before the function\n# This happened!\n# After the function\n```\n\n> Code example: [06-decorators/19-decorators-1.py](06-decorators/19-decorators-1.py)\n\n### Decorators with Timestamps\n\n```python\nimport datetime\n\ndef my_decorator(inner):\n    def inner_decorator():\n        print(datetime.datetime.utcnow())\n        inner()\n        print(datetime.datetime.utcnow())\n    return inner_decorator\n\n@my_decorator\ndef decorated():\n    print(\"This happened!\")\n\ndecorated()\n# 2024-04-17 10:00:00.000001\n# This happened!\n# 2024-04-17 10:00:00.000045\n```\n\n> Code example: [06-decorators/20-decorators-2.py](06-decorators/20-decorators-2.py)\n\n### Decorators with Arguments\n\nWhen the decorated function accepts arguments, the wrapper must accept them too:\n\n```python\nimport datetime\n\ndef my_decorator(inner):\n    def inner_decorator(num_copy):        # mirrors decorated()'s signature\n        print(datetime.datetime.utcnow())\n        inner(int(num_copy) + 1)          # can transform the args\n        print(datetime.datetime.utcnow())\n    return inner_decorator\n\n@my_decorator\ndef decorated(number):\n    print(f\"This happened: {number}\")\n\ndecorated(5)\n# 2024-04-17 10:00:00.000001\n# This happened: 6\n# 2024-04-17 10:00:00.000038\n```\n\n> Code example: [06-decorators/21-decorators-3.py](06-decorators/21-decorators-3.py)\n\n### Universal Decorators with `*args` and `**kwargs`\n\nTo write a decorator that works on any function regardless of signature, use `*args` and `**kwargs`:\n\n```python\ndef decorator(inner):\n    def inner_decorator(*args, **kwargs):\n        print(f\"This function takes {len(args)} positional argument(s)\")\n        inner(*args, **kwargs)\n    return inner_decorator\n\n@decorator\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\n@decorator\ndef add(a, b):\n    print(f\"Sum: {a + b}\")\n\ngreet(\"Alice\")   # This function takes 1 positional argument(s)  /  Hello, Alice!\nadd(3, 4)        # This function takes 2 positional argument(s)  /  Sum: 7\n```\n\n> Code examples: [06-decorators/22-decorators-4.py](06-decorators/22-decorators-4.py), [06-decorators/24-decorators-6.py](06-decorators/24-decorators-6.py)\n\n### Practical Example: Exception Handling Decorator\n\n```python\ndef handle_exceptions(func):\n    def inner(*args, **kwargs):\n        try:\n            return func(*args, **kwargs)\n        except Exception as e:\n            print(f\"An exception was thrown: {e}\")\n    return inner\n\n@handle_exceptions\ndef divide(x, y):\n    return x / y\n\nprint(divide(8, 2))    # 4.0\nprint(divide(8, 0))    # An exception was thrown: division by zero\n```\n\n> Code example: [06-decorators/23-decorators-5.py](06-decorators/23-decorators-5.py)\n\n### Practical Example: Output Multiplier\n\n```python\ndef double(my_func):\n    def inner_func(a, b):\n        return 2 * my_func(a, b)\n    return inner_func\n\n@double\ndef adder(a, b):\n    return a + b\n\n@double\ndef subtractor(a, b):\n    return a - b\n\nprint(adder(10, 20))       # 60  (2 * 30)\nprint(subtractor(6, 1))    # 10  (2 * 5)\n```\n\n> Code example: [06-decorators/25-decorators-7.py](06-decorators/25-decorators-7.py)\n\n### Class Decorators\n\nDecorators can be applied to entire classes. A class decorator receives the class as its argument and should return a class:\n\n```python\ndef honorific(cls):\n    class HonorificCls(cls):\n        def full_name(self):\n            return \"Dr. \" + super().full_name()\n    return HonorificCls\n\n@honorific\nclass Name(object):\n    def __init__(self, first, last):\n        self.first = first\n        self.last = last\n\n    def full_name(self):\n        return f\"{self.first} {self.last}\"\n\nprint(Name(\"Vimal\", \"A.R\").full_name())   # Dr. Vimal A.R\n```\n\n> Code example: [06-decorators/26-class-decorators.py](06-decorators/26-class-decorators.py)\n\n### The `functools.wraps` Best Practice\n\nWrapping a function loses its `__name__` and `__doc__`. Always use `functools.wraps` in production decorators:\n\n```python\nimport functools\n\ndef my_decorator(func):\n    @functools.wraps(func)   # preserves func's metadata\n    def wrapper(*args, **kwargs):\n        print(\"Before\")\n        result = func(*args, **kwargs)\n        print(\"After\")\n        return result\n    return wrapper\n\n@my_decorator\ndef greet():\n    \"\"\"Says hello.\"\"\"\n    print(\"Hello!\")\n\nprint(greet.__name__)   # greet  (not \"wrapper\")\nprint(greet.__doc__)    # Says hello.\n```\n\n---\n\n## 14. Magic Methods (Dunder Methods)\n\n**Magic methods** (also called **dunder methods**, from *d*ouble *under*score) are special methods that Python calls implicitly to implement operators, built-in functions, and protocols. They are the engine behind Python's data model.\n\nAll magic methods follow the pattern `__name__`.\n\n### `__repr__` and `__str__`\n\n- `__repr__`: machine-readable representation, used by `repr()` and in the REPL. Should ideally be a string that recreates the object.\n- `__str__`: human-readable representation, used by `print()` and `str()`. Falls back to `__repr__` if not defined.\n\n```python\nclass PrintList(object):\n    def __init__(self, my_list):\n        self.mylist = my_list\n\n    def __repr__(self):\n        return str(self.mylist)\n\n    def __str__(self):\n        return f\"PrintList({self.mylist})\"\n\npl = PrintList([\"a\", \"b\", \"c\"])\nprint(pl)          # PrintList(['a', 'b', 'c'])  — calls __str__\nprint(repr(pl))    # ['a', 'b', 'c']             — calls __repr__\n```\n\n> Code example: [07-static_class_instance_methods/31-magicmethods-1.py](07-static_class_instance_methods/31-magicmethods-1.py)\n\n### Operators as Magic Methods\n\nEvery operator in Python maps to a magic method. When you write `a + b`, Python calls `a.__add__(b)`.\n\n```python\nmy_list_1 = [\"a\", \"b\", \"c\"]\nmy_list_2 = [\"d\", \"e\", \"f\"]\n\nprint(my_list_1 + my_list_2)            # ['a', 'b', 'c', 'd', 'e', 'f']\nprint(my_list_1.__add__(my_list_2))     # same result — + calls __add__\n```\n\n> Code example: [07-static_class_instance_methods/32-magicmethods-2.py](07-static_class_instance_methods/32-magicmethods-2.py)\n\n### Common Magic Methods Reference\n\n| Category | Method | Triggered by |\n| --- | --- | --- |\n| Representation | `__repr__` | `repr(obj)`, REPL display |\n| Representation | `__str__` | `print(obj)`, `str(obj)` |\n| Lifecycle | `__init__` | `ClassName(...)` |\n| Lifecycle | `__new__` | object allocation (before `__init__`) |\n| Lifecycle | `__del__` | garbage collection |\n| Arithmetic | `__add__` | `a + b` |\n| Arithmetic | `__sub__` | `a - b` |\n| Arithmetic | `__mul__` | `a * b` |\n| Arithmetic | `__truediv__` | `a / b` |\n| Arithmetic | `__floordiv__` | `a // b` |\n| Arithmetic | `__mod__` | `a % b` |\n| Arithmetic | `__pow__` | `a ** b` |\n| Comparison | `__eq__` | `a == b` |\n| Comparison | `__ne__` | `a != b` |\n| Comparison | `__lt__` | `a < b` |\n| Comparison | `__le__` | `a <= b` |\n| Comparison | `__gt__` | `a > b` |\n| Comparison | `__ge__` | `a >= b` |\n| Container | `__len__` | `len(obj)` |\n| Container | `__getitem__` | `obj[key]` |\n| Container | `__setitem__` | `obj[key] = val` |\n| Container | `__delitem__` | `del obj[key]` |\n| Container | `__contains__` | `item in obj` |\n| Container | `__iter__` | `for x in obj` |\n| Container | `__next__` | `next(obj)` |\n| Context manager | `__enter__` | `with obj as x:` |\n| Context manager | `__exit__` | end of `with` block |\n| Callable | `__call__` | `obj(args)` |\n| Attribute access | `__getattr__` | `obj.name` (when not found normally) |\n| Attribute access | `__setattr__` | `obj.name = val` |\n\n### Custom `__eq__` and `__hash__`\n\nIf you define `__eq__`, Python sets `__hash__` to `None` by default (making your object unhashable). Define both together if you need the object to work in sets or as dict keys:\n\n```python\nclass Point(object):\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    def __eq__(self, other):\n        return self.x == other.x and self.y == other.y\n\n    def __hash__(self):\n        return hash((self.x, self.y))\n\np1 = Point(1, 2)\np2 = Point(1, 2)\nprint(p1 == p2)           # True\nprint({p1, p2})           # {Point} — one item, they're equal\n```\n\n---\n\n## 15. Abstract Base Classes\n\nAn **Abstract Base Class (ABC)** is a class that defines a contract — a set of methods that every subclass *must* implement. You cannot instantiate an ABC directly; it exists purely as a template.\n\nABCs are defined using the `abc` module.\n\n### Python 3 Style (Recommended)\n\n```python\nimport abc\n\nclass MyABC(abc.ABC):\n    @abc.abstractmethod\n    def set_val(self, val):\n        pass\n\n    @abc.abstractmethod\n    def get_val(self):\n        pass\n```\n\n### Python 2 Style (Legacy — also works in Python 3)\n\n```python\nimport abc\n\nclass MyABC(object):\n    __metaclass__ = abc.ABCMeta   # Python 2 only\n\n    @abc.abstractmethod\n    def set_val(self, val):\n        return\n\n    @abc.abstractmethod\n    def get_val(self):\n        return\n```\n\n> Code examples: [08-abstract_classes/34-abstractclasses-1.py](08-abstract_classes/34-abstractclasses-1.py), [08-abstract_classes/35-abstractclasses-2.py](08-abstract_classes/35-abstractclasses-2.py)\n\n### Implementing an ABC\n\nA concrete subclass must implement **all** abstract methods, or it will also be abstract (and also uninstantiable):\n\n```python\nimport abc\n\nclass My_ABC_Class(abc.ABC):\n    @abc.abstractmethod\n    def set_val(self, val):\n        pass\n\n    @abc.abstractmethod\n    def get_val(self):\n        pass\n\n\nclass MyClass(My_ABC_Class):\n    def set_val(self, value):\n        self.val = value\n\n    def get_val(self):\n        return self.val\n\n    def hello(self):\n        print(\"I'm not an abstract method — just a bonus!\")\n\n\nmy_obj = MyClass()\nmy_obj.set_val(10)\nprint(my_obj.get_val())   # 10\nmy_obj.hello()\n```\n\n### What Happens When You Miss an Abstract Method\n\n```python\nclass IncompleteClass(My_ABC_Class):\n    def set_val(self, val):\n        self.val = val\n    # get_val not implemented!\n\nobj = IncompleteClass()\n# TypeError: Can't instantiate abstract class IncompleteClass\n# with abstract method get_val\n```\n\n> Code example: [08-abstract_classes/36-abstractclasses-3.py](08-abstract_classes/36-abstractclasses-3.py)\n\n### Why ABCs?\n\n- **Enforce contracts:** guarantee that subclasses provide required behavior.\n- **Document intent:** clearly communicate which methods are part of the interface.\n- **isinstance checks:** `isinstance(obj, MyABC)` returns `True` for any registered implementor, even without actual inheritance (via `MyABC.register(SomeClass)`).\n\nABCs are the Pythonic way to define interfaces. Languages like Java have a formal `interface` keyword; Python uses ABCs.\n\n---\n\n## 16. Method Overloading\n\n**Method overloading** in Python means providing a new or extended implementation of a method that was inherited from a parent class. Python does not support overloading based on argument types the way C++ or Java do — instead, you override and extend.\n\n### Extending a Parent Method\n\nA child class can override a parent's method and then call the parent's version using `super()`:\n\n```python\nimport abc\n\nclass MyClass(abc.ABC):\n    def my_set_val(self, value):\n        self.value = value\n\n    def my_get_val(self):\n        return self.value\n\n    @abc.abstractmethod\n    def print_doc(self):\n        pass\n\n\nclass MyChildClass(MyClass):\n    def my_set_val(self, value):\n        if not isinstance(value, int):\n            value = 0\n        super().my_set_val(value)   # extend, not replace\n\n    def print_doc(self):\n        print(\"Documentation for MyChildClass\")\n\n\nobj = MyChildClass()\nobj.my_set_val(100)\nprint(obj.my_get_val())   # 100\nobj.print_doc()\n```\n\n> Code example: [09-method_overloading/37-method-overloading-1.py](09-method_overloading/37-method-overloading-1.py)\n\n### Overloading to Change Behavior\n\nA child class can completely change how a parent method works. Here `GetSetList` keeps a history of all values set, rather than just the current one:\n\n```python\nclass GetSetParent(abc.ABC):\n    def __init__(self, value):\n        self.val = 0\n\n    def set_val(self, value):\n        self.val = value\n\n    def get_val(self):\n        return self.val\n\n    @abc.abstractmethod\n    def showdoc(self):\n        pass\n\n\nclass GetSetList(GetSetParent):\n    def __init__(self, value=0):\n        self.vallist = [value]   # history of all values\n\n    def get_val(self):\n        return self.vallist[-1]  # return most recent\n\n    def get_vals(self):\n        return self.vallist\n\n    def set_val(self, value):\n        self.vallist.append(value)  # append instead of replace\n\n    def showdoc(self):\n        print(f\"GetSetList, len={len(self.vallist)}, stores value history\")\n```\n\n> Code example: [09-method_overloading/38-method-overloading-2.py](09-method_overloading/38-method-overloading-2.py)\n\n### Overloading Built-in Methods\n\nYou can override built-in container methods by inheriting from built-in types like `list`:\n\n```python\nclass MyList(list):\n    \"\"\"1-indexed list — index 1 maps to position 0.\"\"\"\n\n    def __getitem__(self, index):\n        if index == 0:\n            raise IndexError(\"MyList is 1-indexed; index 0 is invalid\")\n        if index > 0:\n            index -= 1\n        return list.__getitem__(self, index)\n\n    def __setitem__(self, index, value):\n        if index == 0:\n            raise IndexError\n        if index > 0:\n            index -= 1\n        list.__setitem__(self, index, value)\n\n\nx = MyList([\"a\", \"b\", \"c\"])\nprint(x[1])   # \"a\"  — 1-indexed\nprint(x[3])   # \"c\"\n```\n\n> Code example: [09-method_overloading/39-method-overloading-3.py](09-method_overloading/39-method-overloading-3.py)\n\n### Python's Approach to Overloading by Argument Type\n\nPython doesn't have method overloading by signature. Instead, use default arguments, `*args`, or type checking inside the method:\n\n```python\nclass Adder(object):\n    def add(self, a, b=None):\n        if b is None:\n            return a + a   # single arg: double it\n        return a + b\n\nobj = Adder()\nprint(obj.add(5))       # 10\nprint(obj.add(5, 3))    # 8\n```\n\n---\n\n## 17. `super()`\n\nWithout `super()`, a child's overridden method completely replaces the parent's. With `super()`, you can call the parent's version and **extend** rather than replace:\n\n```text\nWithout super()                      With super()\n───────────────                      ────────────\nChildClass.func()                    ChildClass.func()\n  │                                    │\n  └─► \"Child class\"                    ├─► \"Child class\"\n      (parent never runs)              │\n                                       └─► super().func()\n                                             │\n                                             └─► MyClass.func()\n                                                   │\n                                                   └─► \"Parent class\"\n```\n\n`super()` returns a proxy object that delegates method calls to a parent class, following the MRO. It is the correct way to call a parent's method from a child, especially in multiple inheritance scenarios.\n\n### Basic Usage\n\n```python\nclass MyClass(object):\n    def func(self):\n        print(\"Called from the Parent class\")\n\nclass ChildClass(MyClass):\n    def func(self):\n        print(\"Called from the Child class\")\n        super().func()   # delegate to the parent\n\nChildClass().func()\n# Called from the Child class\n# Called from the Parent class\n```\n\n> Code example: [10-super/40-super-1.py](10-super/40-super-1.py)\n\n### Python 2 vs Python 3 Syntax\n\n```python\n# Python 3 — preferred\nsuper().method_name()\n\n# Python 2 — explicit class and self required\nsuper(ClassName, self).method_name()\n```\n\nPython 3 introduced the zero-argument form, which is cleaner and avoids repetition.\n\n### `super()` with `__init__`\n\nWhen a child class has its own `__init__` and needs to also initialize the parent, call `super().__init__()`:\n\n```python\nclass Animal(object):\n    def __init__(self, name):\n        self.name = name\n        print(f\"Animal.__init__ called for {name}\")\n\nclass Dog(Animal):\n    def __init__(self, name, breed):\n        super().__init__(name)    # initialize the Animal part\n        self.breed = breed\n        print(f\"Dog.__init__ called, breed={breed}\")\n\nd = Dog(\"Rex\", \"Labrador\")\n# Animal.__init__ called for Rex\n# Dog.__init__ called, breed=Labrador\nprint(d.name)    # Rex\nprint(d.breed)   # Labrador\n```\n\nForgetting to call `super().__init__()` means the parent's initialization code never runs, leaving the parent's attributes unset.\n\n### `super()` with `__init__` — Call Chain Diagram\n\n```mermaid\nsequenceDiagram\n    participant user as your code\n    participant Dog\n    participant Animal\n\n    user->>Dog: Dog(\"Rex\", \"Labrador\")\n    Dog->>Dog: __init__(name, breed)\n    Dog->>Animal: super().__init__(name)\n    Animal->>Animal: self.name = \"Rex\"\n    Animal-->>Dog: done\n    Dog->>Dog: self.breed = \"Labrador\"\n    Dog-->>user: instance ready\n```\n\n### `super()` in Multiple Inheritance (Cooperative Inheritance)\n\nIn multiple inheritance, `super()` follows the MRO, not just the direct parent. This is called **cooperative multiple inheritance** — each class in the chain calls `super()`, ensuring all classes get initialized:\n\n```python\nclass A(object):\n    def foo(self):\n        print(\"A\")\n\nclass B(A):\n    def foo(self):\n        print(\"B\")\n\nclass C(A):\n    def foo(self):\n        print(\"C\")\n        super().foo()   # calls A.foo per MRO\n\nclass D(B, C):\n    def foo(self):\n        print(\"D\")\n        super().foo()   # calls B.foo per MRO: [D, B, C, A]\n\nd = D()\nd.foo()\n# D\n# B\n# (B doesn't call super, so chain stops)\n```\n\n> Code example: [10-super/42-super-3.py](10-super/42-super-3.py)\n\n**Key insight:** `super()` doesn't mean \"my direct parent\" — it means \"the next class in the MRO.\" This is what makes cooperative inheritance work. For it to work correctly, every class in the diamond should call `super()`.\n\n```mermaid\nflowchart TD\n    D[\"D.foo()\\nprint('D')\\nsuper().foo() ──►\"]\n    B[\"B.foo()\\nprint('B')\\n(no super — chain stops)\"]\n    C[\"C.foo()\\n(not reached because B\\ndoesn't call super)\"]\n    A[\"A.foo()\\n(not reached)\"]\n\n    D --> B\n    B -.->|\"would continue\\nif B called super()\"| C\n    C -.-> A\n\n    style D fill:#4a90d9,color:#fff\n    style B fill:#e67e22,color:#fff\n    style C fill:#aaa,color:#fff\n    style A fill:#aaa,color:#fff\n```\n\n### When to Use `super()`\n\n- Always use `super()` instead of calling the parent class by name directly (`ParentClass.method(self)`). Direct calls break in multiple inheritance scenarios.\n- Call `super().__init__()` in child `__init__` methods to ensure the parent is initialized.\n- Use `super()` when overloading a method but still wanting the parent's behavior.\n\n---\n\n## Summary: The Four Pillars of OOP\n\n| Pillar | What it means in Python |\n| --- | --- |\n| **Encapsulation** | Bundle data + methods together; use `_` / `__` naming conventions and `@property` to control access |\n| **Inheritance** | A subclass acquires the attributes and methods of its parent; use `super()` to extend, not replace |\n| **Polymorphism** | Different classes expose the same interface; Python uses duck typing — no common base required |\n| **Abstraction** | Hide complexity behind a clean interface; use ABCs (`abc.ABC`) to enforce contracts on subclasses |\n\n```text\n ┌─────────────────────────────────────────────────────────────────────────────┐\n │                       The Four Pillars of OOP                              │\n │                                                                             │\n │  ┌─────────────────────┐    ┌─────────────────────────────────────────────┐│\n │  │   ENCAPSULATION     │    │                INHERITANCE                  ││\n │  │                     │    │                                             ││\n │  │  ╔═══════════════╗  │    │   Animal ──────────────────────────────┐   ││\n │  │  ║ private state ║  │    │     │  eat()                           │   ││\n │  │  ╚═══════════════╝  │    │     ▼                                  ▼   ││\n │  │  [ public method ]  │    │   Dog              Cat                     ││\n │  │                     │    │   fetch()          swatstring()            ││\n │  │ Hide internals,     │    │                                            ││\n │  │ expose interface    │    │  Child reuses parent's code                ││\n │  └─────────────────────┘    └────────────────────────────────────────────┘│\n │                                                                             │\n │  ┌─────────────────────────────────────────┐  ┌────────────────────────┐  │\n │  │           POLYMORPHISM                  │  │     ABSTRACTION        │  │\n │  │                                         │  │                        │  │\n │  │  animal.speak()  ──► Dog  →  \"Woof!\"    │  │  class Shape(ABC):     │  │\n │  │                  ──► Cat  →  \"Meow!\"    │  │    @abstractmethod     │  │\n │  │                  ──► Duck →  \"Quack!\"   │  │    def area(): ...     │  │\n │  │                                         │  │                        │  │\n │  │  Same call, different behavior          │  │  Enforce a contract;   │  │\n │  │  based on the actual type               │  │  hide the details      │  │\n │  └─────────────────────────────────────────┘  └────────────────────────┘  │\n └─────────────────────────────────────────────────────────────────────────────┘\n```\n\n---\n\n## Quick Reference: Python OOP Cheat Sheet\n\n```text\nClass definition:       class MyClass(object):\nInstance creation:      obj = MyClass()\nInstance attribute:     self.x = value        (inside a method)\nClass attribute:        x = value             (in class body, outside methods)\nInstance method:        def method(self):\nClass method:           @classmethod / def method(cls):\nStatic method:          @staticmethod / def method():\nProperty getter:        @property / def attr(self):\nProperty setter:        @attr.setter / def attr(self, value):\nAbstract method:        @abc.abstractmethod / def method(self):\nCalling parent method:  super().method()\nMRO inspection:         ClassName.mro()\nMagic method example:   def __repr__(self): return \"...\"\n```\n"
  },
  {
    "path": "SECURITY.md",
    "content": "# Security Policy\n\n## Supported Versions\n\nUse this section to tell people about which versions of your project are\ncurrently being supported with security updates.\n\n| Version | Supported          |\n| ------- | ------------------ |\n| 5.1.x   | :white_check_mark: |\n| 5.0.x   | :x:                |\n| 4.0.x   | :white_check_mark: |\n| < 4.0   | :x:                |\n\n## Reporting a Vulnerability\n\nUse this section to tell people how to report a vulnerability.\n\nTell them where to go, how often they can expect to get an update on a\nreported vulnerability, what to expect if the vulnerability is accepted or\ndeclined, etc.\n"
  }
]