{ "metadata": { "name": "", "signature": "sha256:0b4a52abc3f3246965d1e59241352e867e78e448510ae25e91b7d38dc4dcc202" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 12: A Closer Look at Classes

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.1, Page Number: 274

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class myclass:\n", " __a=None\n", " __b=None\n", " def __init__(self,i,j):\n", " self.__a=i\n", " self.__b=j\n", " def sum(self,x): #Friend function\n", " return sum1(x)\n", " \n", "def sum1(x): \n", " return x._myclass__a +x._myclass__b #accessing private members\n", "\n", "#Variable declaration\n", "n=myclass(3,4)\n", "\n", "#Result\n", "print n.sum(n)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.2, Page Number: 275

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class c1:\n", " __status=None\n", " def set_status(self,state):\n", " self.__status=state\n", " \n", "class c2:\n", " __status=None\n", " def set_status(self,state):\n", " self.status=state\n", " \n", "#Friend function \n", "def idle(a,b):\n", " if a._c1__status or b._c2__status :\n", " return 0\n", " else:\n", " return 1\n", " \n", "#variable declarations\n", "def IDLE(): #Constants\n", " return 0\n", "def INUSE():\n", " return 1\n", "x=c1()\n", "y=c2()\n", "\n", "x.set_status(IDLE())\n", "y.set_status(IDLE())\n", "\n", "if idle(x,y):\n", " print \"Screen Can Be Used.\"\n", " \n", "x.set_status(INUSE())\n", "\n", "if idle(x,y):\n", " print \"Screen Can Be Used.\"\n", "else:\n", " print \"Pop-up In Use.\"\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Screen Can Be Used.\n", "Pop-up In Use.\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.3, Page Number: 277

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def IDLE(): \n", " return 0\n", "def INUSE():\n", " return 1\n", "\n", "class c1:\n", " __status=None\n", " def set_status(self,state):\n", " self.__status=state\n", " def idle(self,b): #now a member of c1\n", " if self.__status or b._c2__status :\n", " return 0\n", " else:\n", " return 1\n", " \n", "class c2:\n", " __status=None #IDLE if off INUSE if on screen\n", " def set_status(self,state):\n", " self.status=state\n", " \n", "#Variable declarations \n", "x=c1()\n", "y=c2()\n", "\n", "x.set_status(IDLE())\n", "y.set_status(IDLE())\n", "\n", "if idle(x,y):\n", " print \"Screen Can Be Used.\"\n", " \n", "x.set_status(INUSE())\n", "\n", "if idle(x,y):\n", " print \"Screen Can Be Used.\"\n", "else:\n", " print \"Pop-up In Use.\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Screen Can Be Used.\n", "Pop-up In Use.\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.4, Page Number: 278

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import time,string\n", "\n", "class timer:\n", " __seconds=None\n", " \n", " def __init__(self,t1,t2=None):\n", " if t2==None:\n", " if isinstance(t1,int): #seconds specified as an integer\n", " self.__seconds=t1\n", " else: #seconds specified as a string\n", " self.__seconds=string.atoi(t1)\n", " else: #time in minutes and seconds\n", " self.__seconds=t1*60+t2\n", " \n", " def run(self):\n", " t1=time.clock()\n", " while (time.clock()-t1)Example 12.5, Page Number: 280

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import time,string\n", "\n", "class timer:\n", " __seconds=None\n", " \n", " def __init__(self,t1,t2=None):\n", " if t2==None:\n", " if isinstance(t1,int): #seconds specified as an integer\n", " self.__seconds=t1\n", " else: #seconds specified as a string\n", " self.__seconds=string.atoi(t1)\n", " else: #time in minutes and seconds\n", " self.__seconds=t1*60+t2\n", " \n", " def run(self):\n", " t1=time.clock()\n", " while (time.clock()-t1)Example 12.6, Page Number: 282

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class myclass:\n", " __a=None #private members\n", " __b=None\n", " def setab(self,i,j): #publc functons\n", " self.__a=i\n", " self.__b=j\n", " def showab(self):\n", " print \"a is \",self.__a\n", " print \"b is \",self.__b\n", "\n", "#Variable declaration\n", "ob1 = myclass()\n", "ob2 = myclass()\n", "\n", "#Intalizing\n", "ob1.setab(10,20)\n", "ob2.setab(0,0)\n", "\n", "print \"ob1 before assignment: \"\n", "ob1.showab()\n", "print \"ob2 before assignment: \"\n", "ob2.showab()\n", "\n", "ob2 = ob1 #assign ob1 to ob2\n", "\n", "#Result\n", "print \"ob1 after assignment: \"\n", "ob1.showab()\n", "print \"ob2 after assignment: \"\n", "ob2.showab()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ob1 before assignment: \n", "a is 10\n", "b is 20\n", "ob2 before assignment: \n", "a is 0\n", "b is 0\n", "ob1 after assignment: \n", "a is 10\n", "b is 20\n", "ob2 after assignment: \n", "a is 10\n", "b is 20\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.7, Page Number: 283

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from copy import deepcopy\n", " \n", "class OBJ:\n", " def set_i(self,x):\n", " self.__i=x\n", " def out_i(self):\n", " print self.__i,\n", " \n", "def f(x):\n", " x=deepcopy(x)\n", " x.out_i() #outputs 10\n", " x.set_i(100) #this affects only local copy\n", " x.out_i() #outputs 100\n", " \n", "#Variable declaration\n", "o=OBJ()\n", "o.set_i(10)\n", "f(o) \n", "o.out_i() #still outputs 10, value of i unchanged\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 100 10\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.8, Page Number: 284

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class myclass: \n", " def __init__(self,i):\n", " self.__val=i\n", " print \"Constructing\"\n", " def __del__(self):\n", " print \"Destructing\"\n", " def getval(self):\n", " return self.__val\n", " \n", "def display(ob):\n", " print ob.getval()\n", "\n", "#Varable declaration\n", "a=myclass(10)\n", "\n", "display(a)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructing\n", "Destructing\n", "10\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.9, Page Number: 286

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "from ctypes import *\n", "\n", "class myclass:\n", " def __init__(self,i):\n", " print \"Allocating p\"\n", " self.p=pointer(c_int(i))\n", " def __del__(self):\n", " print \"Freeing p\"\n", " def getval(self):\n", " return self.p[0]\n", " \n", "def display(ob):\n", " print ob.getval()\n", "\n", "#Variable declaration\n", "a=myclass(10)\n", "\n", "display(a)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Allocating p\n", "10\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.10, Page Number: 287

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "from ctypes import *\n", "class myclass:\n", " def __init__(self,i):\n", " print \"Allocating p\"\n", " self.p=pointer(c_int(i))\n", " def __del__(self):\n", " print \"Freeing p\"\n", " def getval(self):\n", " return self.p[0]\n", " \n", "def display(ob):\n", " print ob[0].getval()\n", "\n", "#Variable declaration\n", "a=[]\n", "a.append(myclass(10))\n", "\n", "display(a)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Freeing p\n", "Allocating p\n", "10\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.11, Page Number: 288

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class sample:\n", " __s=None\n", " def show(self):\n", " print self.__s\n", " def set(self,str):\n", " self.__s=str\n", "\n", "#Return an object of type sample\n", "def input():\n", " str=sample()\n", " instr = \"Hello\" #User input\n", " str.set(instr)\n", " return str\n", "\n", "#Variable declaration\n", "ob=sample()\n", "\n", "#assign returned object to ob\n", "ob=input()\n", "\n", "#Result\n", "ob.show()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.12, Page Number: 289

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class sample:\n", " __s=None\n", " def __init__(self):\n", " self.__s=0\n", " def __del__(self): \n", " print \"Freeing p\"\n", " def show(self):\n", " print self.__s\n", " def set(self,str):\n", " self.__s=str\n", " \n", "#This function takes one object parameter\n", "def input():\n", " str=sample()\n", " instr=\"Hello\" #User input\n", " str.set(instr)\n", " return str\n", "\n", "#Variable declaration\n", "ob=sample()\n", "\n", "#assign returned object to ob\n", "ob=input()\n", "\n", "#Result\n", "ob.show()\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Freeing p\n", "Freeing p\n", "Hello\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.13, Page Number: 292

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class myclass:\n", " __p=None\n", " def __init__(self,i):\n", " if isinstance(i,int):\n", " print \"Allocating p\"\n", " self.__p=i\n", " else:\n", " print \"Copy constructor called\"\n", " self.__p=i.getval()\n", " def __del__(self): \n", " print \"Freeing p\"\n", " def getval(self):\n", " return self.__p\n", " \n", "#This function takes one object parameter\n", "def display(ob):\n", " print ob.getval()\n", "\n", "#Variable declaration\n", "ob=myclass(10)\n", "\n", "#Result\n", "display(ob)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Allocating p\n", "10\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.14, Page Number: 294

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "class myclass:\n", " __p=None\n", " def __init__(self,i):\n", " if isinstance(i,int):\n", " print \"Allocating p\"\n", " self.__p=i\n", " else:\n", " print \"Copy constructor called\"\n", " self.__p=i.getval()\n", " def __del__(self): \n", " print \"Freeing p\"\n", " def getval(self):\n", " return self.__p\n", " \n", "\n", "#Variable declaration\n", "a=myclass(10) #calls normal constructor\n", "b=myclass(a) #calls copy constructor\n", "\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Allocating p\n", "Copy constructor called\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.15, Page Number: 295

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class myclass:\n", " def __init__(self,i=0):\n", " if isinstance(i,int):\n", " print \"Normal constructor\"\n", " else:\n", " print \"Copy constructor\"\n", "\n", "\n", "#Variable declaration\n", "a=myclass() #calls normal constructor\n", "\n", "f=myclass()\n", "a=myclass(f) #Invoke copyconstructor\n", "\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Normal constructor\n", "Normal constructor\n", "Copy constructor\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 12.16, Page Number: 297

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class c1:\n", " def __init__(self):\n", " self.__i=None\n", " def load_i(self,val):\n", " self.__i=val\n", " def get_i(self):\n", " return self.__i\n", " \n", "#Variable declaration \n", "o=c1()\n", "\n", "o.load_i(100)\n", "\n", "#Result\n", "print o.get_i()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }