{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# An Interactive introduction to python $classes$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is an $Object~Oriented~Programming~Language$, which means it manipulates constructs called $\\textit{objects}$. Object is a single data structure and contains data and $\\textit{functions}$. Functions of objects are $\\textit{methods}$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A $CLASS$ is just a way of organizing and producing objects with similar attributes and methods." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#defining a class\n", "class ESO(object):\n", " def __init__(self): # required to initialize the objects that it creates. It takes at least one argument\n", " pass # \"self\", which refers to the object being created." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class ESO(object):\n", " def __init__(self, name, surname): # create the first object, with additional arguments\n", " self.name = name # associate those arguments with the object\n", " self.surname = surname" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "student = ESO(\"Elyar\", \"Sedaghati\") #create an instance of the class" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elyar Sedaghati\n" ] } ], "source": [ "print student.name, student.surname # access the attributes of the object using dot notation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Class Scope" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The scope of a variable is the context in which it is visible to the programme.\n", "1) Global variable: avialable everywhere\n", "2) Member variable: avialabel to mambers of a certain class\n", "3) Instance variable: available to instances of a particular class" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "work_hours = 160 # This is a global variable\n", "class ESO(object):\n", " works_at_ESO = True # This is a member variable\n", " def __init__(self, name, surname, age):\n", " self.name = name\n", " self.surname = surname\n", " self.age = age" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elyar Sedaghati 25 True\n" ] } ], "source": [ "student = ESO('Elyar', 'Sedaghati', 25) # These are instance variables.\n", "print student.name, student.surname, student.age, student.works_at_ESO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When a class has its own functions, those are called the $\\textit{methods}$ of the class." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class ESO(object):\n", " def __init__(self, name, surname, age):\n", " self.name = name\n", " self.surname = surname\n", " self.age = age\n", " def description(self): # \"description\" is a method of the ESO class.\n", " print \"Name: \", self.name\n", " print \"Surname: \", self.surname\n", " print \"Age: \", self.age" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Name: Elyar\n", "Surname: Sedaghati\n", "Age: 25\n" ] } ], "source": [ "student = ESO(\"Elyar\", \"Sedaghati\", 25)\n", "student.description() # calling the description method on the student instance of ESO class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Inheritance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\textit{Inheritance}$: is a process by which a class takes on attributes and methods of another class." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now lets say that we want to create a class that calculates salaray of an ESO employee, based on their position. We would like this class to inheret all the properties of the ESO class, so we do not have to redefine all the common attributes of all the employees." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class SALARY(ESO): # here the SALARY class inherets the attributes and method of the \n", "# def __ini__(self, position): # ESO class.\n", "# self.position = position\n", " def calc_wage(self):\n", " position = raw_input(\"Please enter your position (student, fellow or staff): \").lower()\n", " if position == \"student\":\n", " return \"$\" + str(work_hours * 10) + \" per month\"\n", " elif position == \"fellow\":\n", " return \"$\" + str(work_hours * 60) + \" per month\"\n", " elif position == \"staff\":\n", " return \"$\" + str(work_hours * 200) + \" per month\"\n", " else:\n", " return \"wrong input. Please enter Student, Fellow or Staff.\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [], "source": [ "elyar = SALARY(\"Elyar\", \"Sedaghati\", 25)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter your position (student, fellow or staff): student\n" ] }, { "data": { "text/plain": [ "'$1600 per month'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elyar.calc_wage()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this framework, the top level class is referred to as the $\\textit{parent~class}$, and the lower level classes as $\\textit{child~classes}$. Lets look at a different way a class can inheret its parent properties." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class SALARY2(ESO): # this time we would like the new argument to be initialized by user\n", " def __init__(self, name, surname, age, position): \n", " super(SALARY2, self).__init__(name, surname, age) # we can recall the arguments of \n", " self.position = position\n", " def description(self): # here we would like to override the previous definition of this method\n", " print \"Name: \", self.name # to include the position of the employee.\n", " print \"Surname: \", self.surname\n", " print \"Age: \", self.age\n", " print \"Position: \", self.position" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [], "source": [ "elyar = SALARY(\"elyar\", \"sedaghati\", 25)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Name: elyar\n", "Surname: sedaghati\n", "Age: 25\n" ] } ], "source": [ "elyar.description()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "elyar = SALARY2(\"elyar\", \"sedaghati\", 25, \"student\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Name: elyar\n", "Surname: sedaghati\n", "Age: 25\n", "Position: student\n" ] } ], "source": [ "elyar.description()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also override the way in which data is presented." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class REP(SALARY2):\n", " def __init__(self, name, surname, age, position):\n", " super(REP, self).__init__(name, surname, age, position)\n", " def __repr__(self):\n", " return \"(%s %s is a %s years old %s)\" % (self.name, self.surname, str(self.age), self.position)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "elyar = REP(\"elyar\", \"sedaghati\", 26, \"student\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(elyar sedaghati is a 26 years old student)\n" ] } ], "source": [ "print elyar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Utilizing classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally we can use pre-defined classes in a whole host of ways when are defining new functions, variables, etc." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "elyar = SALARY2(\"elyar\", \"sedaghati\", 25, \"student\")\n", "claudio = SALARY2(\"claudio\", \"melo\", 56, \"staff\")\n", "liz = SALARY2(\"lizette\", \"guzman\", 18, \"fellow\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def ESO_greeting(a,b):\n", " x = a.position\n", " y = b.position\n", " if (x == \"student\" or y == \"fellow\") and y == \"staff\":\n", " return \"%s to %s: Hello your majesty!\" % (a.name, b.name)\n", " elif x == \"student\" and y == \"fellow\":\n", " return \"%s to %s: Hello you over-paid ******!\" % (a.name, b.name)\n", " elif x == \"fellow\" and y == \"student\":\n", " return \"%s to %s: Hello you hard-working student who deserves lots more praise!\" % (a.name, b.name)\n", " else:\n", " return \"%s to %s: get back to work!\" % (a.name, b.name)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'elyar to lizette: Hello you over-paid ******!'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ESO_greeting(elyar, liz)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'lizette to elyar: Hello you hard-working student who deserves lots more praise!'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ESO_greeting(liz, elyar)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'claudio to elyar: get back to work!'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ESO_greeting(claudio, elyar)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'elyar to claudio: Hello your majesty!'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ESO_greeting(elyar, claudio)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }