Created
November 17, 2022 10:28
-
-
Save AhmedAlhallag/469bef44b6990e862512e4734aeaddd0 to your computer and use it in GitHub Desktop.
Desktop/OOP_DEMO_G2.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"id": "5bd7ec1d", | |
"cell_type": "markdown", | |
"source": "# [Procedural-Paradigm]" | |
}, | |
{ | |
"metadata": {}, | |
"id": "abcaf5f3", | |
"cell_type": "markdown", | |
"source": "## Refresher: DEFINITION & CALLING of <u>Functions</u>\n* Where is a function is defined?\n* Where is it called? \"Driver/Client Code\"\n* The Do's and Dont's (print? input? ==> internal Depedencies)\n* Parameters & Arguments: Dependency Injection (D from S.O.L.I.D)" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "4cb46b4c", | |
"cell_type": "code", | |
"source": "\n# Modular Deisgn --> functions \n# 1) DEFINE a fuinction\ndef say_Hello():\n # function block definition \n print(\"Hello\")\n\n# Single responsibility (SR) --> S from SOLID\ndef add_two_v1(n1,n2): #injected paramters: Depeendnecy injection (DI) --> S from SOLID \n return n1+n2\n \n \ndef add_two(): # header/ signature \n n1 = int(input(\"Enter a num: \")) # Code smell: internal dependency (calling a funtion)\n n2 = int(input(\"Enter a num: \"))\n return n1+n2\n\n \n\n\n", | |
"execution_count": 31, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "f945b073", | |
"cell_type": "code", | |
"source": "# ================= Driver/Client Code ==============\n# 2) Calling/Using the function\n\nsay_Hello() # function call \n", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "136b932c", | |
"cell_type": "code", | |
"source": "# Driver code \nadd_two() # function call\n\n", | |
"execution_count": 30, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Enter a num: 1\nEnter a num: 2\n" | |
}, | |
{ | |
"data": { | |
"text/plain": "3" | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "ad8362a6", | |
"cell_type": "code", | |
"source": "n1 = int(input(\"Enter a num: \")) # Code smell: internal dependency (calling a funtion)\nn2 = int(input(\"Enter a num: \"))\nadd_two_v1(n1, n2 )# argunments", | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Enter a num: 1\nEnter a num: 2\n" | |
}, | |
{ | |
"data": { | |
"text/plain": "3" | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"id": "48b939fa", | |
"cell_type": "markdown", | |
"source": "# Object Oriented Programming Paradigm (illustration)" | |
}, | |
{ | |
"metadata": {}, | |
"id": "85aa4448", | |
"cell_type": "markdown", | |
"source": "## [Custom Types] Class with Pre-Initialized Attributes\n* BTW, everyting in Python is based on Classes! PROVE IT \n" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "b890c7b4", | |
"cell_type": "code", | |
"source": "# Pre-built types\n\nprint(type(\"Ahmed\"))\nprint(type(12))\n", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "<class 'str'>\n<class 'int'>\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "7885987f", | |
"cell_type": "code", | |
"source": "# Notations:\n\n# camelCase\nstudentName = \"Ahmed\"\n# Pascal\nStudentName = \"Ahmed\"\n# snake case notation \nstudent_name = \"Ahmed\"", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "33995dde", | |
"cell_type": "code", | |
"source": "\nclass Profile:\n # constructor --> __init__: Special/Magic method (\n # Note: methods are always preceeded by a dot + they are just functions, but within a class )\n def __init__(self):\n self.name = None \n self.bio = None\n self.location= None \n self.followers = None\n self.following = None\n \n \n ", | |
"execution_count": 34, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "c37110bb", | |
"cell_type": "code", | |
"source": "# creating an object\n# generic format \n# <className>()\nprofile1 = Profile() # object creation /class call\nprofile2 = Profile() # object creation /class call", | |
"execution_count": 35, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"id": "983b37b9", | |
"cell_type": "markdown", | |
"source": "### Accessing + Read-Only: Properties/Attributes\n* Remember how you used to access dictionaries? it is slightly different" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "6439b89e", | |
"cell_type": "code", | |
"source": "print(profile1.name)\nprint(profile1.bio)", | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "None\nNone\n" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"id": "f3f83616", | |
"cell_type": "markdown", | |
"source": "### Accessing + Writing: Properties/Attributes" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "dafaae80", | |
"cell_type": "code", | |
"source": "profile1.name = \"Ahmed\"\nprofile1.bio = \"SomethingCool\"\nprint(profile1.name)\nprint(profile1.bio)", | |
"execution_count": 39, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Ahmed\nSomethingCool\n" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"id": "68c800ae", | |
"cell_type": "markdown", | |
"source": "### Inspecting individual objects: \"Pool of Variables\" \n" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "c8d7453e", | |
"cell_type": "code", | |
"source": "print(profile1.__dict__)", | |
"execution_count": 41, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "{'name': 'Ahmed', 'bio': 'SomethingCool', 'location': None, 'followers': None, 'following': None}\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "5c26aff5", | |
"cell_type": "code", | |
"source": "class Profile:\n # constructor --> __init__: Special/Magic method (preceeded by a dot + function inside of a class )\n # initialize \n def __init__(self):\n self.name = None \n self.bio = None\n self.location= None \n self.followers = None\n self.following = None\n ", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "0f61600d", | |
"cell_type": "code", | |
"source": "profile1 = Profile() # object creation /class call\n", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"id": "472d58e0", | |
"cell_type": "markdown", | |
"source": "### External Functions to access object/instance attributes" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "a47bf377", | |
"cell_type": "code", | |
"source": "def display_profile_data(profileObject):\n print(f\"\"\"\n Name: {profileObject.name}, \n Bio: {profileObject.bio}, \n location: {profileObject.location}, \n followers: {profileObject.followers}, \n following: {profileObject.following}, \n \n \"\"\")", | |
"execution_count": 42, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "54f9ad58", | |
"cell_type": "code", | |
"source": "# Driver code\n\n\ndisplay_profile_data(profile1)", | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "\n Name: Ahmed, \n Bio: SomethingCool, \n location: None, \n followers: None, \n following: None, \n \n \n" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"id": "19e8a625", | |
"cell_type": "markdown", | |
"source": "### Activity 1" | |
}, | |
{ | |
"metadata": {}, | |
"id": "8b54dcf8", | |
"cell_type": "markdown", | |
"source": "### What's up with the <font color=red>\"self\"</font> keyword though? (THE CLASS <font color=red><u>\"CONSTRUCTOR\"</u></font> )\n* **Reserved Non-positional** argument\n* Local Variables Discussion\n* Globals to the rescue: \"The Real Source of Evil\"\n" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "0966765b", | |
"cell_type": "code", | |
"source": "class Profile: \n def __init__(self):\n # self: \"binds\" the atrtributes to any object created of the class\n self.name = None \n self.bio = None\n self.location= None \n self.followers = None\n self.following = None", | |
"execution_count": 19, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "9ce11bd3", | |
"cell_type": "code", | |
"source": "class Profile_v2:\n def __init__(self):\n # local variables \n name = None \n bio = None\n location= None \n followers = None\n following = None", | |
"execution_count": 46, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "228040c5", | |
"cell_type": "code", | |
"source": "p1 = Profile()\np2 = Profile_v2()\n\nprint(p1.__dict__)\nprint(p2.__dict__)", | |
"execution_count": 48, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "{'name': None, 'bio': None, 'location': None, 'followers': None, 'following': None}\n{}\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "b95f7b99", | |
"cell_type": "code", | |
"source": "# Global Scope \nx = 1\n\ndef update_x():\n global x # state sharing done WRONG --> better do it via OOP (as object/instance attributes)\n x = 2\n\n\n# Driver \nupdate_x()\nprint(x)", | |
"execution_count": 52, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "2\n" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"id": "08cda4b8", | |
"cell_type": "markdown", | |
"source": "## [Custom Types] Class with Dynamically Initialized Attributes (initialized in Run-Time)\n* Remember paramters & arguments in functions? Same concept but done on the **Constructor's Signature**\n" | |
}, | |
{ | |
"metadata": { | |
"code_folding": [], | |
"trusted": true | |
}, | |
"id": "5137bedb", | |
"cell_type": "code", | |
"source": "class Profile: \n def __init__(self, name, bio, location, followers, following ):\n self.name = name \n self.bio = bio\n self.location= location \n self.followers = followers\n self.following = following", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"id": "e994a800", | |
"cell_type": "code", | |
"source": "#Driver \np1 = Profile(\"AhmedHallag\", \"Bio is Amazing\", \"Cairo\",222,333) # during the object creation, variables were initailzied \n\n", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"id": "eca0e607", | |
"cell_type": "markdown", | |
"source": "### Internal Instance methods to access object/instance attributes\n* \"Instance\" methods using \"instance\" variables/attributes\n" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"id": "dd3b9d1e", | |
"cell_type": "code", | |
"source": "class Profile: \n def __init__(self, name, bio, location, followers, following ):\n self.name = name \n self.bio = bio\n self.location= location \n self.followers = followers\n self.following = following\n\n def display_profile_data(self):\n print(f\"\"\"\n Name: {self.name}, \n Bio: {self.bio}, \n location: {self.location}, \n followers: {self.followers}, \n following: {self.following}, \n\n \"\"\")", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#Driver \np1 = Profile(\"AhmedHallag\", \"Bio is Amazing\", \"Cairo\",222,333) # during the object creation, variables were initailzied \np1.display_profile_data()", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "\n Name: AhmedHallag, \n Bio: Bio is Amazing, \n location: Cairo, \n followers: 222, \n following: 333, \n\n \n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"id": "2bdc980b", | |
"cell_type": "markdown", | |
"source": "#### Recap: \n* What is the difference between \"Functions\" and \"Methods\"?\n* Did we see or use methods before?" | |
}, | |
{ | |
"metadata": {}, | |
"id": "a661d0fb", | |
"cell_type": "markdown", | |
"source": "### Activity 2" | |
}, | |
{ | |
"metadata": {}, | |
"id": "1bccfe3a", | |
"cell_type": "markdown", | |
"source": "### [Class Variables] Tracking Created Objects Universally ?\n* Adding an auto_incremented id to each object that is being created" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "70cf15a2", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "cb767d09", | |
"cell_type": "code", | |
"source": "\n", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"id": "6ef1dcfc", | |
"cell_type": "markdown", | |
"source": "### Activity 3" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "be494d98", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"id": "cf20a2d4", | |
"cell_type": "markdown", | |
"source": "### [Encapsulation] Getters & Setters " | |
}, | |
{ | |
"metadata": {}, | |
"id": "9df216c7", | |
"cell_type": "markdown", | |
"source": "### [Setters] Adding the <font color=green>handle</font> instance attribute....and updating it later on?" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "08720bf6", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "a5a9e637", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "d73811c6", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "66653b39", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"id": "f85e2cdf", | |
"cell_type": "markdown", | |
"source": "### [Getters] What if the profile id was supposed to be \"private\", how do we do that in Python? and later on, how are we supposed to access it?\n" | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "e21f6ddf", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "4c48d084", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": false | |
}, | |
"id": "b4c91ac0", | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.8.8", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Desktop/OOP_DEMO_G2.ipynb", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment