{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Object Oriented Programming (OOP)\n", "- we've been using procedural programming paradigm; focus on functions/procedures\n", "- OOP paradigm is best used in large and complex modern software systems which make it easy to maintain and improve over time\n", "- focus is on creation of objects which contain both data and functionality together under one name\n", "- typically, each class definition corresponds to some object or concept in the real world with some attributes/properties that maintain its state; and the functions/methods correspond to the ways real-world objects interact" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## class\n", "- we've used classes like str, int, float, dict, tuple, etc.\n", "- class keyword lets programmer define their own compound data types\n", "- class is a collection of relevant attributes and methods like real world objects\n", "- e.g., a clas that represents a point in 2-D coordinates\n", "\n", "
\n", "class className:\n", " [statement-1]\n", " .\n", " .\n", " [statement-N]\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### a simple Point class example" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0\n" ] } ], "source": [ "class Point:\n", " pass\n", "a = Point()\n", "a.x = 0\n", "a.y = 0\n", "print(a.x, a.y)\n", "# OK but NOT best practice!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### better class example" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "class Point:\n", " \"\"\"\n", " Point class to represent and manipulate x, y coords\n", " \"\"\"\n", " count = 0 # class variable/attribute\n", " \n", " # constructor to customize the initial state of an object\n", " # first argument refers to the instance being manipulated;\n", " # it is customary to name this parameter self; but can be anything\n", " def __init__(self, xx=0, yy=0):\n", " \"\"\"Create a new point with given x and y coords\"\"\"\n", " # x and y are object variables/attributes\n", " self.x = xx\n", " self.y = yy\n", " Point.count += 1 # increment class variable\n", " \n", " def __del__(self):\n", " Point.count -= 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## class members\n", "- like real world objects, object instances can have both attributes and methods\n", "- use . dot notation to access members\n", "- x and y are attributes of Point class\n", "- __init__() (constructor) and __del__() (destructor) are sepcial methods\n", "- can have as many relevant attributes and methods that help mimic real-world objects" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0\n", "1\n", "2 3\n", "2\n" ] } ], "source": [ "# instantiate an object\n", "p = Point()\n", "# what is the access specifier for attributes?\n", "print(p.x, p.y) \n", "print(Point.count) # access class variable outside class\n", "p1 = Point(2, 3)\n", "print(p1.x, p1.y)\n", "print(Point.count)\n", "\n", "# Run this cell few times and see the value of Point.count\n", "# How do you fix this problem? Use __del__ destructor method.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### visualizing class and instance attributes using pythontutor.com\n", "- https://goo.gl/aGuc4r" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "