Skip to content

Instantly share code, notes, and snippets.

@nobleknightt
Last active June 1, 2021 06:55
Show Gist options
  • Select an option

  • Save nobleknightt/e6a8886fd838e19b4da91c004a6a8415 to your computer and use it in GitHub Desktop.

Select an option

Save nobleknightt/e6a8886fd838e19b4da91c004a6a8415 to your computer and use it in GitHub Desktop.
πŸ”° Object-Oriented Programming in Python
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "oop-python.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMTJjHyynWYdk6hjsxSYEdW",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/er-knight/e6a8886fd838e19b4da91c004a6a8415/oop-python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4aqxr_wWg8MK"
},
"source": [
"[`Corey Schafer's Python OOP playlist on YouTube`](https://youtube.com/playlist?list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sE7PAv7MUNqG"
},
"source": [
"### `Every object is different.`"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5dRt5Q4QN-_F",
"outputId": "7edffcf8-3e7e-46db-ca4c-92858e7832a6"
},
"source": [
"class Employee:\n",
" ...\n",
"\n",
"emp_1 = Employee()\n",
"print(emp_1)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"<__main__.Employee object at 0x7f4a15457cd0>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "31aN9PBrPCVX",
"outputId": "41cb9e60-7d32-4226-8cff-7ae251eade99"
},
"source": [
"emp_2 = Employee()\n",
"print(emp_2)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"<__main__.Employee object at 0x7f4a119fd5d0>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cbNUaHCkcc-h"
},
"source": [
"### `Setting attributes of an object.`"
]
},
{
"cell_type": "code",
"metadata": {
"id": "5FM6GUMycCcM"
},
"source": [
"emp_1.first_name = \"Elon\"\n",
"emp_1.last_name = \"Musk\"\n",
"emp_1.salary = 600000\n",
"emp_1.email = f\"{emp_1.first_name}.{emp_1.last_name}@company.com\""
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rdcHVhQicpmI",
"outputId": "40d8caf4-7a96-4474-a663-426a71bf7c68"
},
"source": [
"print(emp_1.first_name)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"Elon\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4xigt4Wtcuyr",
"outputId": "ec43af91-23fa-4461-8d66-265e9ead6609"
},
"source": [
"print(emp_1.last_name)"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"Musk\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1EKq-LVLcxxT",
"outputId": "c02251b9-37be-4321-94ac-7b07d1fd22cf"
},
"source": [
"print(emp_1.salary)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"600000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dY1RouvLc0Sn",
"outputId": "3c5120f2-7e87-4e85-f75d-e9597be249e6"
},
"source": [
"print(emp_1.email)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MC9cukt_V1F8"
},
"source": [
"### `__init__() receives first argument as instance.`\n",
"\n",
"```py\n",
"emp_1 = Employee(\"Elon\", \"Musk\", 600000)\n",
"``` \n",
"\n",
"*`In this case, __init__() will receive self as emp_1. Then it will set the all of attributes as following:`* \n",
"```py\n",
"emp_1.first_name = first_name\n",
"emp_1.last_name = last_name\n",
"```"
]
},
{
"cell_type": "code",
"metadata": {
"id": "t9AWEzXwPpuI"
},
"source": [
"class Employee:\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
"emp_1 = Employee(\"Jeff\", \"Bezos\", 700000)"
],
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JjdD3FDSdNFx",
"outputId": "6b93e313-ab0e-4b98-ecdd-b03e9fc45b29"
},
"source": [
"print(emp_1.first_name)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"Jeff\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fy-Xa1VvdOpT",
"outputId": "4828d674-08e8-43bb-acf1-85a9b185551e"
},
"source": [
"print(emp_1.last_name)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"Bezos\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "34ZlZfHSdRQb",
"outputId": "d97fda2b-37b6-4a8f-a230-71a73c24d2f1"
},
"source": [
"print(emp_1.salary)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"700000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KBMZFm1CdTxk",
"outputId": "268e6162-ca52-4c98-a075-28e5837899cd"
},
"source": [
"print(emp_1.email)"
],
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": [
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aWPV3Rd1dij5",
"outputId": "3ace2999-1061-446e-8655-e1f6310b47bb"
},
"source": [
"# full name of employee\n",
"\n",
"print(f\"{emp_1.first_name} {emp_1.last_name}\")"
],
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": [
"Jeff Bezos\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "KX-k3u-Md-fP"
},
"source": [
"class Employee:\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
"emp_1 = Employee(\"Jeff\", \"Bezos\", 700000)"
],
"execution_count": 14,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hx49s0NLeRUB",
"outputId": "1e22ab17-49d2-4b81-c02a-8de0d558e25b"
},
"source": [
"print(emp_1.full_name())"
],
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": [
"Jeff Bezos\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2hRTi9Eofjfm"
},
"source": [
"*`↓ This is happening in the background.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wIroVW_dfTVG",
"outputId": "ca6da2e4-26c9-4611-e89f-152ea9b0918c"
},
"source": [
"print(Employee.full_name(emp_1))"
],
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": [
"Jeff Bezos\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "RcZd3v5Tgyhf"
},
"source": [
"class Employee:\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * 1.05) # 5 percent raise\n",
"\n",
"emp_1 = Employee(\"Jeff\", \"Bezos\", 700000)"
],
"execution_count": 17,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UJo1R913hVzr",
"outputId": "adaf0aef-d2ac-425c-cd99-7ddeb6880810"
},
"source": [
"print(emp_1.salary)"
],
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"text": [
"700000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lTLB37pKhZEg",
"outputId": "b9d4d246-e9ba-41eb-d66b-d1b61f3d1d05"
},
"source": [
"emp_1.apply_raise()\n",
"print(emp_1.salary)"
],
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"text": [
"735000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9XTGkEwgiaHu"
},
"source": [
"### `class variables.`"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OUHD_bNOh6Ti"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount) \n",
"\n",
"emp_1 = Employee(\"Jeff\", \"Bezos\", 700000)"
],
"execution_count": 20,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jmOeF1I2lNrD",
"outputId": "846f71ec-4641-4359-a8ba-b98b36b57328"
},
"source": [
"print(emp_1.salary)"
],
"execution_count": 21,
"outputs": [
{
"output_type": "stream",
"text": [
"700000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tYVl58grloW3",
"outputId": "593a5a06-dc04-487d-ba27-f4f06209bb88"
},
"source": [
"emp_1.apply_raise()\n",
"print(emp_1.salary)"
],
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"text": [
"735000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zILJueY3miB2"
},
"source": [
"*`class variables are accessible from class as well as instances.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "32graB_LmRUi",
"outputId": "6866a017-8719-4654-a54f-38419c69f0df"
},
"source": [
"print(Employee.raise_amount)"
],
"execution_count": 23,
"outputs": [
{
"output_type": "stream",
"text": [
"1.05\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jndYwa7UmX4K",
"outputId": "97d08267-bae4-4e20-990e-bd30afbf3360"
},
"source": [
"print(emp_1.raise_amount)"
],
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"text": [
"1.05\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wvO9ZCw_n2dE"
},
"source": [
"*`Accessing class variables with instances. If that variable is not present in instance's namespace, it will search in class's namespace.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "L16-AOH3nFD7",
"outputId": "4a88b0fe-6e7b-4547-bb07-036cb5f3ba02"
},
"source": [
"print(emp_1.__dict__)"
],
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"text": [
"{'first_name': 'Jeff', 'last_name': 'Bezos', 'salary': 735000, 'email': '[email protected]'}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4U3jv30mnZ_P",
"outputId": "22701090-efb0-4621-e266-a19cf44b4789"
},
"source": [
"print(Employee.__dict__)"
],
"execution_count": 26,
"outputs": [
{
"output_type": "stream",
"text": [
"{'__module__': '__main__', 'raise_amount': 1.05, '__init__': <function Employee.__init__ at 0x7f4a119b65f0>, 'full_name': <function Employee.full_name at 0x7f4a119b6710>, 'apply_raise': <function Employee.apply_raise at 0x7f4a119b6680>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': None}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4Uw_rdpZpC4T"
},
"source": [
"*`Modifying class variables with class name, modifies for all instances.`* "
]
},
{
"cell_type": "code",
"metadata": {
"id": "p4z6UkvJoRTA"
},
"source": [
"Employee.raise_amount = 1.06"
],
"execution_count": 27,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3mSATuqQoarx",
"outputId": "83375555-e0f8-4154-d9de-f075ed82b146"
},
"source": [
"print(Employee.raise_amount)"
],
"execution_count": 28,
"outputs": [
{
"output_type": "stream",
"text": [
"1.06\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6DfjNXPZoneN",
"outputId": "caa15b2a-cda5-4fe2-b9f7-e3dbbaad5957"
},
"source": [
"print(emp_1.raise_amount)"
],
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"text": [
"1.06\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yL-rJ3YRp5Jw"
},
"source": [
"*`Modifying class variables with instance, modifies for that instance only i.e., it creates new attribute for that instance with same name as of class variable.`* "
]
},
{
"cell_type": "code",
"metadata": {
"id": "8M2hrUFwoyfH"
},
"source": [
"emp_1.raise_amount = 1.05"
],
"execution_count": 30,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3kOTEJKWo8gv",
"outputId": "34e0fadf-dcb4-4260-b79e-99e746874e46"
},
"source": [
"print(Employee.raise_amount)"
],
"execution_count": 31,
"outputs": [
{
"output_type": "stream",
"text": [
"1.06\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "D3IDgWHeo_CH",
"outputId": "26583484-ac5d-496e-88f8-96bd76e672ee"
},
"source": [
"print(emp_1.raise_amount)"
],
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"text": [
"1.05\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WasQCxQsq1mE",
"outputId": "e970bac1-5a63-4fc0-bb84-3fdc7f46e35d"
},
"source": [
"print(emp_1.__dict__)"
],
"execution_count": 33,
"outputs": [
{
"output_type": "stream",
"text": [
"{'first_name': 'Jeff', 'last_name': 'Bezos', 'salary': 735000, 'email': '[email protected]', 'raise_amount': 1.05}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "u4YnfXNH6ujw"
},
"source": [
"*`In some cases, using class name is more convenient than using instance name (or self). Like in this case:`*\n",
"```py\n",
"Employee.num_of_employees += 1\n",
"```"
]
},
{
"cell_type": "code",
"metadata": {
"id": "rO6JnMvArh4t"
},
"source": [
"class Employee:\n",
" num_of_employees = 0\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" Employee.num_of_employees += 1\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount) "
],
"execution_count": 34,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4IrJOh4h7xvU",
"outputId": "572a79e2-0c39-4830-d9c8-3eb09cc1f1b1"
},
"source": [
"emp_1 = Employee(\"Jeff\", \"Bezos\", 700000)\n",
"print(Employee.num_of_employees)"
],
"execution_count": 35,
"outputs": [
{
"output_type": "stream",
"text": [
"1\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WB6MR3cQsGHZ",
"outputId": "05a32876-e7dd-44c3-a5e7-8d34f2565248"
},
"source": [
"emp_2 = Employee(\"Elon\", \"Musk\", 600000)\n",
"print(Employee.num_of_employees)"
],
"execution_count": 36,
"outputs": [
{
"output_type": "stream",
"text": [
"2\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RYYFYcd7HnO-"
},
"source": [
"### `class methods & static methods.`"
]
},
{
"cell_type": "code",
"metadata": {
"id": "QIPKTMCTHvR_"
},
"source": [
"class Employee:\n",
" num_of_employees = 0\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" Employee.num_of_employees += 1\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount) \n",
"\n",
" @classmethod\n",
" def set_raise_amount(cls, amount):\n",
" cls.raise_amount = amount"
],
"execution_count": 37,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7yl8rVAwJBD1",
"outputId": "0e81ea34-2074-41bb-a68a-3fdaf309393d"
},
"source": [
"print(Employee.raise_amount)"
],
"execution_count": 38,
"outputs": [
{
"output_type": "stream",
"text": [
"1.05\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KnTGYD6QI92n",
"outputId": "1da538d2-2951-43aa-bc4e-e3cadb75eef8"
},
"source": [
"emp_1 = Employee(\"Jeff\", \"Bezos\", 700000)\n",
"print(emp_1.raise_amount)"
],
"execution_count": 39,
"outputs": [
{
"output_type": "stream",
"text": [
"1.05\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LfN9AtX9QDhq"
},
"source": [
"*`set_raise_amount() is classmethod of class Employee. It can be used directly with class as well as with instances.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "qGl9_JtaJfX6",
"outputId": "53592f12-d4cb-475f-8ac4-ac8fb58f3026"
},
"source": [
"Employee.set_raise_amount(1.06)\n",
"print(Employee.raise_amount)"
],
"execution_count": 40,
"outputs": [
{
"output_type": "stream",
"text": [
"1.06\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0jEeGYLZQY74",
"outputId": "059216d4-a45d-4b60-eab0-8507c11dd2bf"
},
"source": [
"emp_1.set_raise_amount(1.05)\n",
"print(Employee.raise_amount)"
],
"execution_count": 41,
"outputs": [
{
"output_type": "stream",
"text": [
"1.05\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "01UeuHBpRXx_"
},
"source": [
"*`class methods can be used as alternative constructors.`*"
]
},
{
"cell_type": "code",
"metadata": {
"id": "GCXeL5MJghK4"
},
"source": [
"class Employee:\n",
" num_of_employees = 0\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" Employee.num_of_employees += 1\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount) \n",
"\n",
" @classmethod\n",
" def set_raise_amount(cls, amount):\n",
" cls.raise_amount = amount\n",
"\n",
" @classmethod\n",
" def from_string(cls, employee_string):\n",
" first_name, last_name, salary = employee_string.split(\"-\")\n",
" return cls(first_name, last_name, salary) "
],
"execution_count": 42,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "IsxHPAa0jaD1"
},
"source": [
"*`from_string() constructs a new object of class cls (i.e Employee).`* \n",
"*`cls(first_name, last_name, salary) is same as Employee(first_name, last_name, salary) `*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "n6t93ZP8jWBD",
"outputId": "a4988647-6da7-46d0-8b64-872ee689a2de"
},
"source": [
"emp_str = \"Bill-Gates-500000\"\n",
"emp_1 = Employee.from_string(emp_str)\n",
"print(emp_1.full_name())"
],
"execution_count": 43,
"outputs": [
{
"output_type": "stream",
"text": [
"Bill Gates\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iJ9lHDeek57q"
},
"source": [
"*`While class methods automatically pass class (i.e. cls) as the first argument & regular methods automatically pass instance (i.e. self) as the first argument, static methods doesn't pass anything automatically.`*\n",
"\n",
"**`Note`** *`If a method doesn't access class variables or instance variable, that method is going to be a static method.`*"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WSqGMZ4omusd"
},
"source": [
"class Employee:\n",
" num_of_employees = 0\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" Employee.num_of_employees += 1\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount) \n",
"\n",
" @classmethod\n",
" def set_raise_amount(cls, amount):\n",
" cls.raise_amount = amount\n",
"\n",
" @classmethod\n",
" def from_string(cls, employee_string):\n",
" first_name, last_name, salary = employee_string.split(\"-\")\n",
" return cls(first_name, last_name, salary) \n",
"\n",
" @staticmethod\n",
" def is_workday(day):\n",
" if day.weekday() == 5 or day.weekday() == 6:\n",
" return False\n",
" return True"
],
"execution_count": 44,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2lsn-6-7n9z2",
"outputId": "6020fb52-0af7-41ba-d5c1-f922672325e0"
},
"source": [
"import datetime\n",
"\n",
"my_date = datetime.date(2021, 5, 19) # Wednesday\n",
"print(Employee.is_workday(my_date))"
],
"execution_count": 45,
"outputs": [
{
"output_type": "stream",
"text": [
"True\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hpQ4QxhRojJL",
"outputId": "97d2b910-df40-41ee-ee2b-0b096629917c"
},
"source": [
"my_date = datetime.date(2021, 5, 16) # Sunday\n",
"print(Employee.is_workday(my_date))"
],
"execution_count": 46,
"outputs": [
{
"output_type": "stream",
"text": [
"False\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BB8Olp7pqSSg"
},
"source": [
"### `Inheritance`"
]
},
{
"cell_type": "code",
"metadata": {
"id": "cb81PCdBqgZR"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
"class Developer(Employee):\n",
" ..."
],
"execution_count": 47,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4XW3e1qoXHC0",
"outputId": "ec34f4eb-cd47-4059-9f87-3782e7f11abb"
},
"source": [
"dev_1 = Employee(\"Bill\", \"Gates\", 500000)\n",
"print(dev_1.email)"
],
"execution_count": 48,
"outputs": [
{
"output_type": "stream",
"text": [
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kM-72W18XUgI"
},
"source": [
"*`child class (Developer) inherites all methods and attributes of parent class (Employee).`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "B2GusIbVXJDu",
"outputId": "9811694b-faca-4ab2-cc4f-3a8e0fb64565"
},
"source": [
"dev_1 = Developer(\"Bill\", \"Gates\", 500000)\n",
"print(dev_1.email)"
],
"execution_count": 49,
"outputs": [
{
"output_type": "stream",
"text": [
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4bJH80cHYV1e"
},
"source": [
"*`dev_1 instance will search for email attribute in Developer class. When it can't find it in Developer class, it will search in it's perent (i.e. Employee) class. This is called Method resolution order.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mkt5O9QKYBiR",
"outputId": "1febb143-579d-4898-8975-45da66bf8b3c"
},
"source": [
"help(Developer)"
],
"execution_count": 50,
"outputs": [
{
"output_type": "stream",
"text": [
"Help on class Developer in module __main__:\n",
"\n",
"class Developer(Employee)\n",
" | Developer(first_name, last_name, salary)\n",
" | \n",
" | Method resolution order:\n",
" | Developer\n",
" | Employee\n",
" | builtins.object\n",
" | \n",
" | Methods inherited from Employee:\n",
" | \n",
" | __init__(self, first_name, last_name, salary)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | apply_raise(self)\n",
" | \n",
" | full_name(self)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors inherited from Employee:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data and other attributes inherited from Employee:\n",
" | \n",
" | raise_amount = 1.05\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OlYn3T5dZj0o",
"outputId": "e904c2e4-94b2-43cf-8eb6-0afd657d1c1e"
},
"source": [
"print(dev_1.salary)"
],
"execution_count": 51,
"outputs": [
{
"output_type": "stream",
"text": [
"500000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "S-7r0BNVaPwE",
"outputId": "7836c32e-7225-4d36-d7ce-197c9c028b96"
},
"source": [
"dev_1.apply_raise()\n",
"print(dev_1.salary)"
],
"execution_count": 52,
"outputs": [
{
"output_type": "stream",
"text": [
"525000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "NFzjj9xFbUvM"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
"class Developer(Employee):\n",
" raise_amount = 1.10\n",
"\n",
" def __init__(self, first_name, last_name, salary, programming_lang):\n",
" super().__init__(first_name, last_name, salary)\n",
" self.programming_lang = programming_lang"
],
"execution_count": 53,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZixbzDPodB5J",
"outputId": "147430e8-9ad3-4b50-dff9-b0ba7756bf21"
},
"source": [
"dev_1 = Developer(\"Bill\", \"Gates\", 500000, \"C++\")\n",
"print(dev_1.email)\n",
"print(dev_1.programming_lang)"
],
"execution_count": 54,
"outputs": [
{
"output_type": "stream",
"text": [
"[email protected]\n",
"C++\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "MbmIUdZLdpmR"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
"class Developer(Employee):\n",
" raise_amount = 1.10\n",
"\n",
" def __init__(self, first_name, last_name, salary, programming_lang):\n",
" super().__init__(first_name, last_name, salary)\n",
" self.programming_lang = programming_lang\n",
"\n",
"class Manager(Employee):\n",
"\n",
" def __init__(self, first_name, last_name, salary, employees=None):\n",
" super().__init__(first_name, last_name, salary)\n",
" if employees is None:\n",
" self.employees = []\n",
" else:\n",
" self.employees = employees\n",
"\n",
" def add_employee(self, employee):\n",
" if employee not in self.employees:\n",
" self.employees.append(employee)\n",
"\n",
" def remove_employee(self, employee):\n",
" if employee in self.employees:\n",
" self.employees.remove(employee)\n",
"\n",
" def print_employees(self):\n",
" for employee in self.employees:\n",
" print(f\"--> {employee.full_name()}\")"
],
"execution_count": 55,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "R7B1P_lhgBoV",
"outputId": "622d5097-1ab4-4086-ff35-2d80b0165aad"
},
"source": [
"dev_1 = Developer(\"Steve\", \"Jobs\", 500000, \"C\")\n",
"\n",
"man_1 = Manager(\"Tim\", \"Cook\", 700000, [dev_1])\n",
"print(man_1.email)"
],
"execution_count": 56,
"outputs": [
{
"output_type": "stream",
"text": [
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "slIX4lRFg987",
"outputId": "a4b35292-2c8e-4962-9002-4f49037a1724"
},
"source": [
"man_1.print_employees()"
],
"execution_count": 57,
"outputs": [
{
"output_type": "stream",
"text": [
"--> Steve Jobs\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WM1kAeuXhn23",
"outputId": "f8129800-e31d-4992-b3b3-dff5471b45c5"
},
"source": [
"dev_2 = Developer(\"Jack\", \"Dorsey\", 400000, \"JavaScript\")\n",
"\n",
"man_1.add_employee(dev_2)\n",
"man_1.print_employees()"
],
"execution_count": 58,
"outputs": [
{
"output_type": "stream",
"text": [
"--> Steve Jobs\n",
"--> Jack Dorsey\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "R8iv4MKriU-Z",
"outputId": "19acbebc-c87c-434f-eb00-e7a045312e2f"
},
"source": [
"man_1.remove_employee(dev_1)\n",
"man_1.print_employees()"
],
"execution_count": 59,
"outputs": [
{
"output_type": "stream",
"text": [
"--> Jack Dorsey\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1ZPL2FAxjWHW"
},
"source": [
"*`isinstance().`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "l_RqcvIgi1S9",
"outputId": "76fcf688-dfbe-4486-d657-dc80c68284c9"
},
"source": [
"print(isinstance(man_1, Manager))"
],
"execution_count": 60,
"outputs": [
{
"output_type": "stream",
"text": [
"True\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "om9B1ARLi_n-",
"outputId": "07133f16-7263-4140-9d50-cf8db1770ec8"
},
"source": [
"print(isinstance(man_1, Employee))"
],
"execution_count": 61,
"outputs": [
{
"output_type": "stream",
"text": [
"True\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4Ja2MC6TjL0r",
"outputId": "1e8d55b9-089c-4a1d-c5f8-a8ddac66ebc0"
},
"source": [
"print(isinstance(man_1, Developer))"
],
"execution_count": 62,
"outputs": [
{
"output_type": "stream",
"text": [
"False\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ib4VAGtzjhrb"
},
"source": [
"*`issubclass().`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NDylyg6sjkbW",
"outputId": "a3d2517b-a600-4c99-817d-c268f402ea16"
},
"source": [
"print(issubclass(Developer, Employee))"
],
"execution_count": 63,
"outputs": [
{
"output_type": "stream",
"text": [
"True\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_goqrDoWj57d",
"outputId": "13f12455-901b-484d-a866-1fd8a1b2c1db"
},
"source": [
"print(issubclass(Manager, Employee))"
],
"execution_count": 64,
"outputs": [
{
"output_type": "stream",
"text": [
"True\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WNgjHTSMkAy7",
"outputId": "de98f5a9-039c-4639-831e-87f802c8712c"
},
"source": [
"print(issubclass(Manager, Developer))"
],
"execution_count": 65,
"outputs": [
{
"output_type": "stream",
"text": [
"False\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hOAnVorYktbC"
},
"source": [
"### `Special methods or Magic methods or Dunder methods.` "
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kf2Q1SRjlxsk",
"outputId": "4fb1d955-e2ee-4e96-ad53-0dc6ab537464"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
"emp_1 = Employee(\"Larry\", \"Page\", 500000)\n",
"print(emp_1)"
],
"execution_count": 66,
"outputs": [
{
"output_type": "stream",
"text": [
"<__main__.Employee object at 0x7f4a11a1e050>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3ikdbf1wlA5G",
"outputId": "1464baba-ad45-427f-d4a0-9d7dfbe1bdd7"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
" def __repr__(self):\n",
" return f\"Employee('{self.first_name}', '{self.last_name}', '{self.salary}')\"\n",
"\n",
"emp_1 = Employee(\"Larry\", \"Page\", 500000)\n",
"print(emp_1)"
],
"execution_count": 67,
"outputs": [
{
"output_type": "stream",
"text": [
"Employee('Larry', 'Page', '500000')\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0msims4toBRG",
"outputId": "88fb2854-4c9a-475a-ecb0-88118f849da3"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
" def __repr__(self):\n",
" return f\"Employee('{self.first_name}', '{self.last_name}', '{self.salary}')\"\n",
"\n",
" def __str__(self):\n",
" return f\"{self.full_name()} - {self.email}\"\n",
"\n",
"emp_1 = Employee(\"Larry\", \"Page\", 500000)\n",
"print(emp_1)"
],
"execution_count": 68,
"outputs": [
{
"output_type": "stream",
"text": [
"Larry Page - [email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ARJ5HRxWoeBi",
"outputId": "a824c79f-66b5-4487-bb49-f94bf739e8ad"
},
"source": [
"print(repr(emp_1))\n",
"print(str(emp_1))"
],
"execution_count": 69,
"outputs": [
{
"output_type": "stream",
"text": [
"Employee('Larry', 'Page', '500000')\n",
"Larry Page - [email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R1WuoLPFo4v1"
},
"source": [
"*`↓ This is happening in the background.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iFasHJXioj9y",
"outputId": "ae247356-d8b0-417b-ddf0-b397a2407c80"
},
"source": [
"print(emp_1.__repr__())\n",
"print(emp_1.__str__())"
],
"execution_count": 70,
"outputs": [
{
"output_type": "stream",
"text": [
"Employee('Larry', 'Page', '500000')\n",
"Larry Page - [email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "kZs5AJCSsh9f"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
" def __repr__(self):\n",
" return f\"Employee('{self.first_name}', '{self.last_name}', '{self.salary}')\"\n",
"\n",
" def __str__(self):\n",
" return f\"{self.full_name()} - {self.email}\"\n",
"\n",
" def __add__(self, other):\n",
" return self.salary + other.salary"
],
"execution_count": 71,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lnBkzJgHtMwH",
"outputId": "71e3bac4-bfd4-47f7-e2b8-e2c63a5ebcfc"
},
"source": [
"emp_1 = Employee(\"Larry\", \"Page\", 500000)\n",
"emp_2 = Employee(\"Jack\", \"Dorsey\", 400000)\n",
"print(emp_1 + emp_2)"
],
"execution_count": 72,
"outputs": [
{
"output_type": "stream",
"text": [
"900000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vg6uYQ6Htpwe"
},
"source": [
"*`↓ This is happening in the background.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Nt_irdDbtjPN",
"outputId": "75ab2c9e-97fe-4f61-86b5-34806e12e0c3"
},
"source": [
"print(emp_1.__add__(emp_2))"
],
"execution_count": 73,
"outputs": [
{
"output_type": "stream",
"text": [
"900000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "MF1ALt60yKrB"
},
"source": [
"class Employee:\n",
" raise_amount = 1.05\n",
"\n",
" def __init__(self, first_name, last_name, salary):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.salary = salary\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" def apply_raise(self):\n",
" self.salary = int(self.salary * self.raise_amount)\n",
"\n",
" def __repr__(self):\n",
" return f\"Employee('{self.first_name}', '{self.last_name}', '{self.salary}')\"\n",
"\n",
" def __str__(self):\n",
" return f\"{self.full_name()} - {self.email}\"\n",
"\n",
" def __add__(self, other):\n",
" return self.salary + other.salary\n",
"\n",
" def __len__(self):\n",
" return len(self.full_name())"
],
"execution_count": 74,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VD6dein9yTPG",
"outputId": "07292a70-2c45-4454-9acf-4bc5fa0735bc"
},
"source": [
"emp_1 = Employee(\"Larry\", \"Page\", 500000)\n",
"print(len(emp_1))"
],
"execution_count": 75,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OANR7D0YybSm"
},
"source": [
"*`↓ This is happening in the background.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_2ZEgSfoyaWi",
"outputId": "6f6ede59-be24-446b-ace0-d6d9bee2cd50"
},
"source": [
"print(emp_1.__len__())"
],
"execution_count": 76,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "of1zAagIzfh9"
},
"source": [
"### `property decorators - getters, setters and deleters`"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lL2hf2G-zuOp",
"outputId": "11890b02-14eb-427d-e943-68392f0fd29c"
},
"source": [
"class Employee:\n",
"\n",
" def __init__(self, first_name, last_name):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
" self.email = f\"{first_name}.{last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
"emp_1 = Employee(\"John\", \"Doe\")\n",
"print(emp_1.first_name)\n",
"print(emp_1.last_name)\n",
"print(emp_1.email)"
],
"execution_count": 77,
"outputs": [
{
"output_type": "stream",
"text": [
"John\n",
"Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TNkkxVcc1Zb-"
},
"source": [
"*`updating first_name doesn't update it in email.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ix30btaf1Mip",
"outputId": "8f44dc12-1b32-4798-a0d6-ba00023c2b0c"
},
"source": [
"emp_1.first_name = \"Jim\"\n",
"print(emp_1.first_name)\n",
"print(emp_1.last_name)\n",
"print(emp_1.email)"
],
"execution_count": 78,
"outputs": [
{
"output_type": "stream",
"text": [
"Jim\n",
"Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "m4PlERsV1lC4",
"outputId": "26feb4f1-32ad-4fb2-a585-0740a821ef27"
},
"source": [
"class Employee:\n",
"\n",
" def __init__(self, first_name, last_name):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
"\n",
" def email(self):\n",
" return f\"{self.first_name}.{self.last_name}@company.com\"\n",
"\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
"emp_1 = Employee(\"John\", \"Doe\")\n",
"print(emp_1.first_name)\n",
"print(emp_1.last_name)\n",
"print(emp_1.email())"
],
"execution_count": 79,
"outputs": [
{
"output_type": "stream",
"text": [
"John\n",
"Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KMfrERi_27k4",
"outputId": "86570208-b07b-4c58-b9c6-7bdce660ba89"
},
"source": [
"emp_1.first_name = \"Jim\"\n",
"print(emp_1.first_name)\n",
"print(emp_1.last_name)\n",
"print(emp_1.email())"
],
"execution_count": 80,
"outputs": [
{
"output_type": "stream",
"text": [
"Jim\n",
"Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hh-ZNRgQ4Qeu"
},
"source": [
"*`using property decorator, methods can be accessed as attributes.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "spDQPkjY3z5-",
"outputId": "e7c8fa43-ca69-4d1f-94c9-0de40196f590"
},
"source": [
"class Employee:\n",
"\n",
" def __init__(self, first_name, last_name):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
"\n",
" @property\n",
" def email(self):\n",
" return f\"{self.first_name}.{self.last_name}@company.com\"\n",
"\n",
" @property\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
"emp_1 = Employee(\"John\", \"Doe\")\n",
"print(emp_1.full_name)\n",
"print(emp_1.email)"
],
"execution_count": 81,
"outputs": [
{
"output_type": "stream",
"text": [
"John Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "RtDQ_tGWTWK-"
},
"source": [
"class Employee:\n",
"\n",
" def __init__(self, first_name, last_name):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
"\n",
" @property\n",
" def email(self):\n",
" return f\"{self.first_name}.{self.last_name}@company.com\"\n",
"\n",
" @property\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" @full_name.setter\n",
" def full_name(self, name):\n",
" first_name, last_name = name.split()\n",
" self.first_name = first_name\n",
" self.last_name = last_name"
],
"execution_count": 82,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aODPRzISUH3-",
"outputId": "f0148dfb-06e0-41ea-a611-b1562221f6e7"
},
"source": [
"emp_1 = Employee(\"John\", \"Doe\")\n",
"print(emp_1.full_name)\n",
"print(emp_1.email)"
],
"execution_count": 83,
"outputs": [
{
"output_type": "stream",
"text": [
"John Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vi0HiuT9XEI8"
},
"source": [
"*`using full_name.setter decorator, full_name method can be used as attribute.`*"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mcYTHHZSW7N7",
"outputId": "0ab52e46-18ea-4426-9e56-cff1f45d3711"
},
"source": [
"emp_1.full_name = \"Jane Smith\"\n",
"print(emp_1.full_name)\n",
"print(emp_1.email)"
],
"execution_count": 84,
"outputs": [
{
"output_type": "stream",
"text": [
"Jane Smith\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "LU-__Wc3YW90"
},
"source": [
"class Employee:\n",
"\n",
" def __init__(self, first_name, last_name):\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
"\n",
" @property\n",
" def email(self):\n",
" return f\"{self.first_name}.{self.last_name}@company.com\"\n",
"\n",
" @property\n",
" def full_name(self):\n",
" return f\"{self.first_name} {self.last_name}\"\n",
"\n",
" @full_name.setter\n",
" def full_name(self, name):\n",
" first_name, last_name = name.split()\n",
" self.first_name = first_name\n",
" self.last_name = last_name\n",
"\n",
" @full_name.deleter\n",
" def full_name(self):\n",
" print(\"Deleted Name!\")\n",
" self.first_name = None\n",
" self.last_name = None"
],
"execution_count": 85,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LGbconc5ZrOm",
"outputId": "c4e8b757-7bdb-4429-eba6-09e0ba51e1c4"
},
"source": [
"emp_1 = Employee(\"John\", \"Doe\")\n",
"print(emp_1.full_name)\n",
"print(emp_1.email)"
],
"execution_count": 86,
"outputs": [
{
"output_type": "stream",
"text": [
"John Doe\n",
"[email protected]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HPpUBDK1ZyxI",
"outputId": "ee5c9f4f-e394-4479-a1f1-d59eb58bba0c"
},
"source": [
"del emp_1.full_name"
],
"execution_count": 87,
"outputs": [
{
"output_type": "stream",
"text": [
"Deleted Name!\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment