{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic OOPS and Advanced Concepts" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " NAME :E1\n", " SALARY :45000\n", " ROLE :t1\n", " \n", "\n", " NAME :E2\n", " SALARY :50000\n", " ROLE :t2\n", " \n", "8 8\n", "10\n", "10 20\n", "20 10\n", "this is a good string : good1\n", "this is a good string : good2\n" ] } ], "source": [ "class Employee:\n", " number_of_leaves = 8\n", " \n", " def __init__(self,name,salary,role):\n", " self.name = name\n", " self.salary = salary\n", " self.role = role\n", " \n", " def print_details(self):\n", " \n", " print(f\"\"\"\n", " NAME :{self.name}\n", " SALARY :{self.salary}\n", " ROLE :{self.role}\n", " \"\"\")\n", " \n", " @classmethod\n", " def change_leaves(cls,new_leaves):\n", " cls.number_of_leaves = new_leaves\n", " \n", " @classmethod\n", " def instance_with_dash(cls,params_dash):\n", " return cls(*params_dash.split(\"-\"))\n", " \n", " @staticmethod\n", " def print_good_string(string):\n", " print(f\"this is a good string : {string}\")\n", " \n", " \n", "\n", " \n", "e1 = Employee(\"E1\",45000,\"t1\")\n", "e2 = Employee.instance_with_dash(\"E2-50000-t2\")\n", "\n", "e1.print_details()\n", "e2.print_details()\n", "\n", "print(e1.number_of_leaves, e2.number_of_leaves)\n", "\n", "e1.number_of_leaves = 10\n", "print(e1.number_of_leaves)\n", "\n", "Employee.number_of_leaves = 20\n", "\n", "print(e1.number_of_leaves, e2.number_of_leaves)\n", "\n", "e2.number_of_leaves = 10\n", "print(Employee.number_of_leaves, e2.number_of_leaves)\n", "\n", "e1.print_good_string(\"good1\")\n", "Employee.print_good_string(\"good2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single Inheritance" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class Player:\n", " def __init__(self,name,sports):\n", " self.name = name\n", " self.sports = sports\n", " \n", " def print_details(self):\n", " print(f\"\"\"\n", " NAME :{self.name}\n", " SPORTS :{self.sports}\n", " \"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### without super()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " NAME :e1\n", " SALARY :35000\n", " ROLE :P1\n", " \n", "C++\n" ] } ], "source": [ "class Programmer(Employee,Player):\n", " \n", " language =\"C++\"\n", " \n", " def print_language(self):\n", " print(self.language)\n", " \n", "p1 = Programmer(\"e1\",35000,\"P1\")\n", "p1.print_details()\n", "p1.print_language()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "__init__() takes 3 positional arguments but 4 were given", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlanguage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mp1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mProgrammer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"e1\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m35000\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"P1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mp1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_details\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mp1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_language\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: __init__() takes 3 positional arguments but 4 were given" ] } ], "source": [ "# sequence matters\n", "\n", "## this will throw error\n", "class Programmer(Player,Employee):\n", " \n", " language =\"C++\"\n", " \n", " def print_language(self):\n", " print(self.language)\n", " \n", "p1 = Programmer(\"e1\",35000,\"P1\")\n", "p1.print_details() \n", "p1.print_language()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " NAME :e1\n", " SPORTS :['tennis', 'cricket']\n", " \n", "C++\n" ] } ], "source": [ "class Programmer(Player,Employee):\n", " \n", " language =\"C++\"\n", " \n", " def print_language(self):\n", " print(self.language)\n", " \n", "p1 = Programmer(\"e1\",[\"tennis\",\"cricket\"])\n", "p1.print_details()\n", "p1.print_language()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Heirarchy in descending order \n", "\n", "instance variable child > child class variable > parent instance variable > parent class variable " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a instance variable in class B'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class A:\n", " classvar1 = \"this is a class vairable in class A\"\n", " \n", " def __init__(self):\n", " self.classvar1 = \"this is a instance variable in class A\"\n", " \n", "class B(A):\n", " classvar1 = \"this is a class variable in class B\"\n", " \n", " def __init__(self):\n", " self.classvar1 = \"this is a instance variable in class B\"\n", "\n", " \n", "\n", "b = B()\n", "\n", "b.classvar1\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a instance variable in class A'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class A:\n", " classvar1 = \"this is a class vairable in class A\"\n", " \n", " def __init__(self):\n", " self.classvar1 = \"this is a instance variable in class A\"\n", " \n", "class B(A):\n", " classvar1 = \"this is a class variable in class B\"\n", " \n", "# def __init__(self):\n", "# self.classvar1 = \"this is a instance variable in class B\"\n", "\n", " \n", "\n", "b = B()\n", "\n", "b.classvar1\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a class variable in class B'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class A:\n", " classvar1 = \"this is a class vairable in class A\"\n", " \n", "# def __init__(self):\n", "# self.classvar1 = \"this is a instance variable in class A\"\n", " \n", "class B(A):\n", " classvar1 = \"this is a class variable in class B\"\n", " \n", "# def __init__(self):\n", "# self.classvar1 = \"this is a instance variable in class B\"\n", "\n", " \n", "\n", "b = B()\n", "\n", "b.classvar1\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a class vairable in class A'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class A:\n", " classvar1 = \"this is a class vairable in class A\"\n", " \n", "# def __init__(self):\n", "# self.classvar1 = \"this is a instance variable in class A\"\n", " \n", "class B(A):\n", " pass\n", "# classvar1 = \"this is a class variable in class B\"\n", " \n", "# def __init__(self):\n", "# self.classvar1 = \"this is a instance variable in class B\"\n", "\n", " \n", "\n", "b = B()\n", "\n", "b.classvar1\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## super()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a instance variable in class B'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class A:\n", " classvar1 = \"this is a class vairable in class A\"\n", " \n", " def __init__(self):\n", " self.var1 = \"this is a instance variable in class A\"\n", " \n", "class B(A):\n", " classvar1 = \"this is a class variable in class B\"\n", " \n", " def __init__(self):\n", " super().__init__()\n", " self.var1 = \"this is a instance variable in class B\" ## overridden by child\n", "\n", " \n", "\n", "b = B()\n", "\n", "b.var1\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a instance variable in class A'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class A:\n", " classvar1 = \"this is a class vairable in class A\"\n", " \n", " def __init__(self):\n", " self.var1 = \"this is a instance variable in class A\"\n", " \n", "class B(A):\n", " classvar1 = \"this is a class variable in class B\"\n", " \n", " def __init__(self):\n", " self.var1 = \"this is a instance variable in class B\"\n", " super().__init__() ## overridden by parent \n", " \n", "\n", "b = B()\n", "\n", "b.var1\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Diamond shape problem\n", "\n", " -----------> A <----------- \n", " | |\n", " | |\n", " | |\n", " B C\n", " ^ ^\n", " | |\n", " | |\n", " | |\n", " ------------`D`------------" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CLASS B\n" ] } ], "source": [ "class A:\n", " def func(self):\n", " print(\"CLASS A\")\n", "class B(A):\n", " def func(self):\n", " print(\"CLASS B\")\n", "class C(A):\n", " def func(self):\n", " print(\"CLASS C\")\n", "class D(B,C):\n", " pass\n", "\n", "a = A()\n", "b = B()\n", "c = C()\n", "d = D()\n", "\n", "d.func()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CLASS C\n" ] } ], "source": [ "\n", "class A:\n", " def func(self):\n", " print(\"CLASS A\")\n", "class B(A):\n", " def func(self):\n", " print(\"CLASS B\")\n", "class C(A):\n", " def func(self):\n", " print(\"CLASS C\")\n", "class D(C,B):\n", " pass\n", "\n", "a = A()\n", "b = B()\n", "c = C()\n", "d = D()\n", "\n", "d.func()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operator overloading and dunder methods(underscore methods)\n", "\n", "https://docs.python.org/3/library/operator.html\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'Employee' and 'Employee'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0me2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mEmployee\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"E2\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m40000\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"SDE1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0me2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Employee' and 'Employee'" ] } ], "source": [ "class Employee:\n", " number_of_leaves = 8\n", " \n", " def __init__(self,name,salary,role):\n", " self.name = name\n", " self.salary = salary\n", " self.role = role\n", " \n", " def print_details(self):\n", " \n", " print(f\"\"\"\n", " NAME :{self.name}\n", " SALARY :{self.salary}\n", " ROLE :{self.role}\n", " \"\"\")\n", " \n", " @classmethod\n", " def change_leaves(cls,new_leaves):\n", " cls.number_of_leaves = new_leaves\n", " \n", " @classmethod\n", " def instance_with_dash(cls,params_dash):\n", " return cls(*params_dash.split(\"-\"))\n", " \n", " @staticmethod\n", " def print_good_string(string):\n", " print(f\"this is a good string : {string}\")\n", " \n", "e1 = Employee(\"E1\",45000,\"SDE1\")\n", "e2 = Employee(\"E2\",40000,\"SDE1\")\n", "\n", "print(e1+e2)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "85000\n", "1.125\n" ] } ], "source": [ "class Employee:\n", " number_of_leaves = 8\n", " \n", " def __init__(self,name,salary,role):\n", " self.name = name\n", " self.salary = salary\n", " self.role = role\n", " \n", " def print_details(self):\n", " \n", " print(f\"\"\"\n", " NAME :{self.name}\n", " SALARY :{self.salary}\n", " ROLE :{self.role}\n", " \"\"\")\n", " \n", " @classmethod\n", " def change_leaves(cls,new_leaves):\n", " cls.number_of_leaves = new_leaves\n", " \n", " @classmethod\n", " def instance_with_dash(cls,params_dash):\n", " return cls(*params_dash.split(\"-\"))\n", " \n", " @staticmethod\n", " def print_good_string(string):\n", " print(f\"this is a good string : {string}\")\n", " \n", " ## create a dunder method to work on add operation \n", " \n", " def __add__(self,other):\n", " \n", " return self.salary + other.salary\n", " \n", " def __truediv__(self,other):\n", " \n", " return self.salary / other.salary\n", " \n", "e1 = Employee(\"E1\",45000,\"SDE1\")\n", "e2 = Employee(\"E2\",40000,\"SDE1\")\n", "\n", "print(e1+e2)\n", "print(e1/e2)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__weakref__',\n", " 'change_leaves',\n", " 'instance_with_dash',\n", " 'number_of_leaves',\n", " 'print_details',\n", " 'print_good_string']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(Employee)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.Employee at 0x7f747cba7370>" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e1 ## here it is showing default" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Employee('E1',45000,'SDE1')\n", "Employee('E1',45000,'SDE1')\n" ] } ], "source": [ "class Employee:\n", " number_of_leaves = 8\n", " \n", " def __init__(self,name,salary,role):\n", " self.name = name\n", " self.salary = salary\n", " self.role = role\n", " \n", " def print_details(self):\n", " \n", " print(f\"\"\"\n", " NAME :{self.name}\n", " SALARY :{self.salary}\n", " ROLE :{self.role}\n", " \"\"\")\n", " \n", " @classmethod\n", " def change_leaves(cls,new_leaves):\n", " cls.number_of_leaves = new_leaves\n", " \n", " @classmethod\n", " def instance_with_dash(cls,params_dash):\n", " return cls(*params_dash.split(\"-\"))\n", " \n", " @staticmethod\n", " def print_good_string(string):\n", " print(f\"this is a good string : {string}\")\n", " \n", " ## create a dunder method to work on add operation \n", " \n", " def __add__(self,other):\n", " \n", " return self.salary + other.salary\n", " \n", " def __truediv__(self,other):\n", " \n", " return self.salary / other.salary\n", " \n", " def __repr__(self):\n", " return f\"\"\"Employee('{self.name}',{self.salary},'{self.role}')\"\"\"\n", " \n", "e1 = Employee(\"E1\",45000,\"SDE1\")\n", "e2 = Employee(\"E2\",40000,\"SDE1\")\n", "\n", "print(e1)\n", "print(repr(e1)) ##overridden repr" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " NAME :E1\n", " SALARY :45000\n", " ROLE :SDE1\n", " \n", "\n", " NAME :E1\n", " SALARY :45000\n", " ROLE :SDE1\n", " \n", "Employee('E1',45000,'SDE1')\n" ] } ], "source": [ "class Employee:\n", " number_of_leaves = 8\n", " \n", " def __init__(self,name,salary,role):\n", " self.name = name\n", " self.salary = salary\n", " self.role = role\n", " \n", " def print_details(self):\n", " \n", " print(f\"\"\"\n", " NAME :{self.name}\n", " SALARY :{self.salary}\n", " ROLE :{self.role}\n", " \"\"\")\n", " \n", " @classmethod\n", " def change_leaves(cls,new_leaves):\n", " cls.number_of_leaves = new_leaves\n", " \n", " @classmethod\n", " def instance_with_dash(cls,params_dash):\n", " return cls(*params_dash.split(\"-\"))\n", " \n", " @staticmethod\n", " def print_good_string(string):\n", " print(f\"this is a good string : {string}\")\n", " \n", " ## create a dunder method to work on add operation \n", " \n", " def __add__(self,other):\n", " \n", " return self.salary + other.salary\n", " \n", " def __truediv__(self,other):\n", " \n", " return self.salary / other.salary\n", " \n", " def __repr__(self):\n", " return f\"\"\"Employee('{self.name}',{self.salary},'{self.role}')\"\"\"\n", " \n", " def __str__(self):\n", " return f\"\"\"\n", " NAME :{self.name}\n", " SALARY :{self.salary}\n", " ROLE :{self.role}\n", " \"\"\"\n", " \n", "e1 = Employee(\"E1\",45000,\"SDE1\")\n", "e2 = Employee(\"E2\",40000,\"SDE1\")\n", "\n", "print(e1) ##overridden repr with str\n", "print(str(e1))\n", "print(repr(e1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Abstract Base Class" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "from abc import ABC,abstractmethod\n", "\n", "class Shape(ABC):\n", " \n", " @abstractmethod\n", " def print_area(self):\n", " pass " ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Can't instantiate abstract class Square with abstract methods print_area", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSquare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class Square with abstract methods print_area" ] } ], "source": [ " \n", "class Square(Shape):\n", " \n", " def __init__(self,side):\n", " self.side = side\n", " \n", " \n", "s = Square(10)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ " \n", "class Square(Shape):\n", " \n", " def __init__(self,side):\n", " self.side = side\n", " \n", " def print_area(self):\n", " \n", " print(self.side**2)\n", " \n", " \n", "s = Square(10)\n", "s.print_area()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Can't instantiate abstract class Shape with abstract methods print_area", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mShape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# cant create\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class Shape with abstract methods print_area" ] } ], "source": [ "Shape() # cant create " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## setter and property decorator " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant Maheshwari\n", "Nishant.Maheshwari@faloola.com\n", "Nishant Baheti\n", "Nishant.Maheshwari@faloola.com\n" ] } ], "source": [ "class Subscriber:\n", " \n", " def __init__(self,fname,lname):\n", " self.fname = fname\n", " self.lname = lname\n", " self._email = f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", " def name(self):\n", " return f\"{self.fname} {self.lname}\"\n", " \n", " def email(self):\n", " return self._email\n", "s = Subscriber(\"Nishant\",\"Maheshwari\")\n", "print(s.name())\n", "print(s.email())\n", "\n", "s.lname = \"Baheti\"\n", "print(s.name())\n", "print(s.email()) ## email will not change" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant Maheshwari\n" ] }, { "ename": "TypeError", "evalue": "'str' object is not callable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSubscriber\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Nishant\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Maheshwari\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0memail\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlname\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Baheti\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: 'str' object is not callable" ] } ], "source": [ "class Subscriber:\n", " \n", " def __init__(self,fname,lname):\n", " self.fname = fname\n", " self.lname = lname\n", "# self.email = f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", " def name(self):\n", " return f\"{self.fname} {self.lname}\"\n", " \n", " @property\n", " def email(self):\n", " return f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", "s = Subscriber(\"Nishant\",\"Maheshwari\")\n", "print(s.name())\n", "print(s.email())\n", "\n", "s.lname = \"Baheti\"\n", "print(s.name())\n", "print(s.email()) ## email changed but set as property which is not callable" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant Maheshwari\n", "Nishant.Maheshwari@faloola.com\n", "Nishant Baheti\n", "Nishant.Baheti@faloola.com\n" ] } ], "source": [ "class Subscriber:\n", " \n", " def __init__(self,fname,lname):\n", " self.fname = fname\n", " self.lname = lname\n", "# self.email = f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", " def name(self):\n", " return f\"{self.fname} {self.lname}\"\n", " \n", " @property\n", " def email(self):\n", " return f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", "s = Subscriber(\"Nishant\",\"Maheshwari\")\n", "print(s.name())\n", "print(s.email)\n", "\n", "s.lname = \"Baheti\"\n", "print(s.name())\n", "print(s.email) ## email changed" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant Maheshwari\n", "Nishant.Maheshwari@faloola.com\n", "Nishant Baheti\n", "Nishant.Baheti@faloola.com\n" ] }, { "ename": "AttributeError", "evalue": "can't set attribute", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0memail\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m## email changed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0memail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"foo.bar@faloola.com\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: can't set attribute" ] } ], "source": [ "class Subscriber:\n", " \n", " def __init__(self,fname,lname):\n", " self.fname = fname\n", " self.lname = lname\n", "# self.email = f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", " def name(self):\n", " return f\"{self.fname} {self.lname}\"\n", " \n", " @property\n", " def email(self):\n", " return f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", "s = Subscriber(\"Nishant\",\"Maheshwari\")\n", "print(s.name())\n", "print(s.email)\n", "\n", "s.lname = \"Baheti\"\n", "print(s.name())\n", "print(s.email) ## email changed\n", "\n", "s.email = \"foo.bar@faloola.com\"" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant Maheshwari\n", "Nishant.Maheshwari@faloola.com\n", "Nishant Baheti\n", "Nishant.Baheti@faloola.com\n", "foo bar\n", "foo.bar@faloola.com\n" ] } ], "source": [ "class Subscriber:\n", " \n", " def __init__(self,fname,lname):\n", " self.fname = fname\n", " self.lname = lname\n", " \n", " def name(self):\n", " return f\"{self.fname} {self.lname}\"\n", " \n", " @property\n", " def email(self):\n", " return f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", " @email.setter\n", " def email(self,new_email):\n", " name = new_email.split(\"@\")[0]\n", " name_l = name.split(\".\")\n", " self.fname = name_l[0]\n", " self.lname = name_l[1]\n", " \n", " \n", " \n", "s = Subscriber(\"Nishant\",\"Maheshwari\")\n", "print(s.name())\n", "print(s.email)\n", "\n", "s.lname = \"Baheti\"\n", "print(s.name())\n", "print(s.email) ## email changed\n", "\n", "\n", "s.email = \"foo.bar@faloola.com\"\n", "print(s.name())\n", "print(s.email) " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant Maheshwari\n", "Nishant.Maheshwari@faloola.com\n", "Nishant Baheti\n", "Nishant.Baheti@faloola.com\n", "foo bar\n", "foo.bar@faloola.com\n", "Email is not set\n" ] } ], "source": [ "class Subscriber:\n", " \n", " def __init__(self,fname,lname):\n", " self.fname = fname\n", " self.lname = lname\n", " \n", " def name(self):\n", " return f\"{self.fname} {self.lname}\"\n", " \n", " @property\n", " def email(self):\n", " if self.fname is None or self.lname is None:\n", " return \"Email is not set\"\n", " return f\"{self.fname}.{self.lname}@faloola.com\"\n", " \n", " @email.setter\n", " def email(self,new_email):\n", " name = new_email.split(\"@\")[0]\n", " name_l = name.split(\".\")\n", " self.fname = name_l[0]\n", " self.lname = name_l[1]\n", " \n", " @email.deleter\n", " def email(self):\n", " self.fname = None\n", " self.lname = None\n", " \n", "s = Subscriber(\"Nishant\",\"Maheshwari\")\n", "print(s.name())\n", "print(s.email)\n", "\n", "s.lname = \"Baheti\"\n", "print(s.name())\n", "print(s.email) ## email changed\n", "\n", "\n", "s.email = \"foo.bar@faloola.com\"\n", "print(s.name())\n", "print(s.email) \n", "\n", "del s.email\n", "print(s.email) \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Object instrospection" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"hello\")" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140138285539376" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(\"hello\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__getnewargs__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'capitalize',\n", " 'casefold',\n", " 'center',\n", " 'count',\n", " 'encode',\n", " 'endswith',\n", " 'expandtabs',\n", " 'find',\n", " 'format',\n", " 'format_map',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isascii',\n", " 'isdecimal',\n", " 'isdigit',\n", " 'isidentifier',\n", " 'islower',\n", " 'isnumeric',\n", " 'isprintable',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper',\n", " 'join',\n", " 'ljust',\n", " 'lower',\n", " 'lstrip',\n", " 'maketrans',\n", " 'partition',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rpartition',\n", " 'rsplit',\n", " 'rstrip',\n", " 'split',\n", " 'splitlines',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'title',\n", " 'translate',\n", " 'upper',\n", " 'zfill']" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(\"hello\")" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('__add__', ), ('__class__', ), ('__contains__', ), ('__delattr__', ), ('__dir__', ), ('__doc__', \"str(object='') -> str\\nstr(bytes_or_buffer[, encoding[, errors]]) -> str\\n\\nCreate a new string object from the given object. If encoding or\\nerrors is specified, then the object must expose a data buffer\\nthat will be decoded using the given encoding and error handler.\\nOtherwise, returns the result of object.__str__() (if defined)\\nor repr(object).\\nencoding defaults to sys.getdefaultencoding().\\nerrors defaults to 'strict'.\"), ('__eq__', ), ('__format__', ), ('__ge__', ), ('__getattribute__', ), ('__getitem__', ), ('__getnewargs__', ), ('__gt__', ), ('__hash__', ), ('__init__', ), ('__init_subclass__', ), ('__iter__', ), ('__le__', ), ('__len__', ), ('__lt__', ), ('__mod__', ), ('__mul__', ), ('__ne__', ), ('__new__', ), ('__reduce__', ), ('__reduce_ex__', ), ('__repr__', ), ('__rmod__', ), ('__rmul__', ), ('__setattr__', ), ('__sizeof__', ), ('__str__', ), ('__subclasshook__', ), ('capitalize', ), ('casefold', ), ('center', ), ('count', ), ('encode', ), ('endswith', ), ('expandtabs', ), ('find', ), ('format', ), ('format_map', ), ('index', ), ('isalnum', ), ('isalpha', ), ('isascii', ), ('isdecimal', ), ('isdigit', ), ('isidentifier', ), ('islower', ), ('isnumeric', ), ('isprintable', ), ('isspace', ), ('istitle', ), ('isupper', ), ('join', ), ('ljust', ), ('lower', ), ('lstrip', ), ('maketrans', ), ('partition', ), ('replace', ), ('rfind', ), ('rindex', ), ('rjust', ), ('rpartition', ), ('rsplit', ), ('rstrip', ), ('split', ), ('splitlines', ), ('startswith', ), ('strip', ), ('swapcase', ), ('title', ), ('translate', ), ('upper', ), ('zfill', )]\n" ] } ], "source": [ "import inspect \n", "\n", "print(inspect.getmembers(\"Hello\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }