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

Chapter 19: Run-Time Type ID and the Casting Operators

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

Example 19.1, Page Number: 453

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class myclass:\n", " pass\n", "\n", "#Variable declaration\n", "i=j=0\n", "f=0.0\n", "ob=myclass()\n", "\n", "print \"The type of i is:\",type(i).__name__\n", "print \"The type of f is:\",type(f).__name__\n", "print \"The type of ob is:\",ob.__class__.__name__\n", "print \"\\n\"\n", "\n", "if type(i)==type(j):\n", " print \"The types of i and j are the same\"\n", " \n", "if not(type(i)==type(f)):\n", " print \"The types of i and f are not the same\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The type of i is: int\n", "The type of f is: float\n", "The type of ob is: myclass\n", "\n", "\n", "The types of i and j are the same\n", "The types of i and f are not the same\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.2, Page Number: 454

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Base:\n", " pass\n", "class Derived1(Base):\n", " pass\n", "class Derived2(Base):\n", " pass\n", "\n", "#Variable declaration\n", "baseob=Base()\n", "p=[Base()]\n", "ob1=Derived1()\n", "ob2=Derived2()\n", "\n", "\n", "p[0]=baseob\n", "print \"p is pointing to an object of type\",\n", "print p[0].__class__.__name__\n", "\n", "p[0]=ob1\n", "print \"p is pointing to an object of type\",\n", "print p[0].__class__.__name__\n", "\n", "p[0]=ob2\n", "print \"p is pointing to an object of type\",\n", "print p[0].__class__.__name__" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "p is pointing to an object of type Base\n", "p is pointing to an object of type Derived1\n", "p is pointing to an object of type Derived2\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.3, Page Number: 455

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Base:\n", " pass\n", "class Derived1(Base):\n", " pass\n", "class Derived2(Base):\n", " pass\n", "\n", "def WhatType(ob):\n", " print \"ob is referencing an object of type\",\n", " print ob.__class__.__name__\n", " \n", " \n", "#Variable declaration\n", "baseob=Base()\n", "p=[Base()]\n", "ob1=Derived1()\n", "ob2=Derived2()\n", "\n", "WhatType(baseob)\n", "WhatType(ob1)\n", "WhatType(ob2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ob is referencing an object of type Base\n", "ob is referencing an object of type Derived1\n", "ob is referencing an object of type Derived2\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.4, Page Number: 456

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import random\n", "\n", "class figure:\n", " def __init__(self,i,j):\n", " self._x=i\n", " self._y=j\n", " \n", "class triangle(figure):\n", " def __init__(self,i,j):\n", " figure.__init__(self,i,j)\n", " def area(self):\n", " return self._x*0.5*self._y\n", " \n", "class rectangle(figure):\n", " def __init__(self,i,j):\n", " figure.__init__(self,i,j)\n", " def area(self):\n", " return self._x*self._y\n", " \n", "class circle(figure):\n", " def __init__(self,i,j=0):\n", " figure.__init__(self,i,j)\n", " def area(self):\n", " return self._x*self._x*3.14\n", " \n", "def factory():\n", " i=random.randint(0,2)\n", " if i==0:\n", " return circle(10.0)\n", " elif i==1:\n", " return triangle(10.1,5.3)\n", " elif i==2:\n", " return rectangle(4.3,5.7)\n", " \n", "\n", "t=c=r=0 \n", "p=[None]\n", "\n", "#generate and count objects\n", "for i in range(10):\n", " p[0]=factory() #generate an object\n", " print \"Object is \",p[0].__class__.__name__,\". \",\n", " #count it\n", " if p[0].__class__.__name__==triangle.__name__:\n", " t+=1\n", " if p[0].__class__.__name__==rectangle.__name__:\n", " r+=1\n", " if p[0].__class__.__name__==circle.__name__:\n", " c+=1\n", " #display its area\n", " print \"Area is\",p[0].area()\n", "\n", "print \"Objects generated:\"\n", "print \"Triangles:\",t\n", "print \"Rectangles:\",r\n", "print \"Circles:\",c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Object is circle . Area is 314.0\n", "Object is rectangle . Area is 24.51\n", "Object is rectangle . Area is 24.51\n", "Object is circle . Area is 314.0\n", "Object is rectangle . Area is 24.51\n", "Object is circle . Area is 314.0\n", "Object is rectangle . Area is 24.51\n", "Object is triangle . Area is 26.765\n", "Object is rectangle . Area is 24.51\n", "Object is circle . Area is 314.0\n", "Objects generated:\n", "Triangles: 1\n", "Rectangles: 5\n", "Circles: 4\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.5, Page Number: 456

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "class myclass:\n", " def __init__(self,i):\n", " self.__a=i\n", " \n", "o1=myclass(10)\n", "o2=myclass(9)\n", "o3=myclass(7.2)\n", "\n", "print \"Type of o1 is\",o1.__class__.__name__\n", "\n", "print \"Type of o2 is\",o2.__class__.__name__\n", "\n", "print \"Type of o3 is\",o3.__class__.__name__\n", "\n", "print\n", "\n", "if o1.__class__.__name__==o2.__class__.__name__:\n", " print \"o1 and o2 are the same type\"\n", " \n", "if o1.__class__.__name__==o3.__class__.__name__:\n", " print \"Error\"\n", "else:\n", " print \"o1 and o3 are different types\"\n", "\n", "#This prints error because python doesnt use templates." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type of o1 is myclass\n", "Type of o2 is myclass\n", "Type of o3 is myclass\n", "\n", "o1 and o2 are the same type\n", "Error\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.6, Page Number: 460

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import random\n", "\n", "class figure:\n", " def __init__(self,i,j):\n", " self._x=i\n", " self._y=j\n", " \n", "class triangle(figure):\n", " def __init__(self,i,j):\n", " figure.__init__(self,i,j)\n", " def area(self):\n", " return self._x*0.5*self._y\n", " \n", "class rectangle(figure):\n", " def __init__(self,i,j):\n", " figure.__init__(self,i,j)\n", " def area(self):\n", " return self._x*self._y\n", " \n", "class circle(figure):\n", " def __init__(self,i,j=0):\n", " figure.__init__(self,i,j)\n", " def area(self):\n", " return self._x*self._x*3.14\n", " \n", "def factory():\n", " i=random.randint(0,2)\n", " if i==0:\n", " return circle(10.0)\n", " elif i==1:\n", " return triangle(10.1,5.3)\n", " elif i==2:\n", " return rectangle(4.3,5.7)\n", " \n", "\n", "t=c=r=0 \n", "p=[None]\n", "\n", "#generate and count objects\n", "for i in range(10):\n", " p[0]=factory() #generate an object\n", " print \"Object is \",p[0].__class__.__name__,\". \",\n", " #count it\n", " if p[0].__class__.__name__==triangle.__name__:\n", " t+=1\n", " if p[0].__class__.__name__==rectangle.__name__:\n", " r+=1\n", " if p[0].__class__.__name__==circle.__name__:\n", " c+=1\n", " #display its area\n", " print \"Area is\",p[0].area()\n", "\n", "print \"Objects generated:\"\n", "print \"Triangles:\",t\n", "print \"Rectangles:\",r\n", "print \"Circles:\",c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Object is triangle . Area is 26.765\n", "Object is circle . Area is 314.0\n", "Object is circle . Area is 314.0\n", "Object is rectangle . Area is 24.51\n", "Object is rectangle . Area is 24.51\n", "Object is rectangle . Area is 24.51\n", "Object is circle . Area is 314.0\n", "Object is circle . Area is 314.0\n", "Object is rectangle . Area is 24.51\n", "Object is triangle . Area is 26.765\n", "Objects generated:\n", "Triangles: 2\n", "Rectangles: 4\n", "Circles: 4\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.7, Page Number: 463

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Base:\n", " def f(self):\n", " print \"Inside Base\"\n", " \n", "class Derived(Base):\n", " def f(self):\n", " print \"Inside Derived\"\n", " \n", "bp=[Base()] #pointer to base\n", "b_ob=Base() \n", "dp=[Derived()] #pointer to derived\n", "d_ob=Derived()\n", "\n", "dp[0]=d_ob\n", "if dp[0]:\n", " print \"Cast from Derived * to Derived * OK.\"\n", " dp[0].f()\n", "else:\n", " print \"Error\"\n", "print\n", "\n", "bp[0]=d_ob\n", "if bp[0]:\n", " print \"Cast from Derived * to Base * OK.\"\n", " bp[0].f()\n", "else:\n", " print \"Error\"\n", "print\n", "\n", "bp[0]=b_ob\n", "if bp[0]:\n", " print \"Cast from Base * to Base * OK.\"\n", " bp[0].f()\n", "else:\n", " print \"Error\"\n", "print\n", "\n", "dp[0]=b_ob\n", "if dp[0]:\n", " print \"Error\"\n", "else:\n", " print \"Cast from Base * to Derived * not OK.\"\n", "print\n", "\n", "bp[0]=d_ob #bp points to Derived object\n", "dp[0]=bp[0]\n", "if dp[0]:\n", " print \"Cast bp to a Derived * OK.\"\n", " print \"because bp is really pointing\\n\",\n", " print \"to a Derived object.\"\n", " dp[0].f()\n", "else:\n", " print \"Error\"\n", "print\n", "\n", "bp[0]=b_ob #bp points to Base object\n", "dp[0]=bp[0]\n", "if dp[0]:\n", " print \"Error\"\n", "else:\n", " print \"Now casting bp to a Derived *\\n\",\n", " print \"is not OK because bp is really\\n\",\n", " print \" pointing to a Base object.\"\n", "print\n", "\n", "dp[0]=d_ob #dp points to Derived object\n", "bp[0]=dp[0]\n", "if bp[0]:\n", " print \"Casting dp to a Base * is OK.\"\n", " bp[0].f()\n", "else:\n", " print \"Error\"\n", "print\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Cast from Derived * to Derived * OK.\n", "Inside Derived\n", "\n", "Cast from Derived * to Base * OK.\n", "Inside Derived\n", "\n", "Cast from Base * to Base * OK.\n", "Inside Base\n", "\n", "Error\n", "\n", "Cast bp to a Derived * OK.\n", "because bp is really pointing\n", "to a Derived object.\n", "Inside Derived\n", "\n", "Error\n", "\n", "Casting dp to a Base * is OK.\n", "Inside Derived\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.8, Page Number: 465

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Base:\n", " def f(self):\n", " pass\n", " \n", "class Derived(Base):\n", " def derivedOnly(self):\n", " print \"Is a Derived Object\"\n", " \n", "bp=[Base()] #pointer to base\n", "b_ob=Base() \n", "dp=[Derived()] #pointer to derived\n", "d_ob=Derived()\n", "\n", "#Use typeid\n", "\n", "bp[0]=b_ob\n", "if bp[0].__class__.__name__==Derived.__name__:\n", " dp[0]=bp[0]\n", " dp[0].derivedOnly()\n", "else:\n", " print \"Cast from Base to Derived failed.\"\n", " \n", "bp[0]=d_ob\n", "if bp[0].__class__.__name__==Derived.__name__:\n", " dp[0]=bp[0]\n", " dp[0].derivedOnly()\n", "else:\n", " print \"Error, cast should work!\"\n", " \n", "#Use dynamic_cast\n", "\n", "bp[0]=b_ob\n", "dp[0]=bp[0]\n", "if dp[0].__class__.__name__==Derived.__name__:\n", " dp[0].derivedOnly()\n", "else:\n", " print \"Cast from Base to Derived failed.\"\n", " \n", "bp[0]=d_ob\n", "dp[0]=bp[0]\n", "if dp:\n", " dp[0].derivedOnly()\n", "else:\n", " print \"Error, cast should work!\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Cast from Base to Derived failed.\n", "Is a Derived Object\n", "Cast from Base to Derived failed.\n", "Is a Derived Object\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.9, Page Number: 467

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def f(p):\n", " v=p\n", " v[0]=100\n", " \n", "#Variable declaration\n", "x=[]\n", "\n", "x.append(99)\n", "print \"x before call:\",x[0]\n", "f(x)\n", "print \"x after call:\",x[0]\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x before call: 99\n", "x after call: 100\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.10, Page Number: 468

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "f=199.22\n", "\n", "i=f\n", "print i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "199.22\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 19.11, Page Number: 469

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i=0 #int\n", "p=\"This is a string\"\n", "\n", "i=p #cast pointer to integer \n", "\n", "#Result\n", "print i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a string\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }