|
8 | 8 | {
|
9 | 9 | "cells": [
|
10 | 10 | {
|
11 |
| - "cell_type": "heading", |
12 |
| - "level": 1, |
| 11 | + "cell_type": "markdown", |
13 | 12 | "metadata": {},
|
14 | 13 | "source": [
|
15 |
| - "Introduction to programming for Geoscientists (through Python)" |
| 14 | + "#Introduction to programming for Geoscientists (through Python)\n", |
| 15 | + "###[Gerard Gorman](http://www.imperial.ac.uk/people/g.gorman), [Christian Jacobs](http://www.imperial.ac.uk/people/c.jacobs10)" |
16 | 16 | ]
|
17 | 17 | },
|
18 | 18 | {
|
|
21 | 21 | "source": [
|
22 | 22 | "#Lecture 6: Introduction to classes\n",
|
23 | 23 | "\n",
|
24 |
| - "[Gerard J. Gorman](http://www.imperial.ac.uk/people/g.gorman) <g.gorman@imperial.ac.uk>\n", |
| 24 | + "Learning objectives: \n", |
25 | 25 | "\n",
|
26 |
| - "Learning objectives: You will learn how to create your own **objects** in Python and develop **member functions** for these new data types." |
| 26 | + "* Learn how to create your own **objects** in Python and develop **member functions** for these new data types." |
27 | 27 | ]
|
28 | 28 | },
|
29 | 29 | {
|
|
48 | 48 | ]
|
49 | 49 | },
|
50 | 50 | {
|
51 |
| - "cell_type": "raw", |
52 |
| - "metadata": {}, |
53 |
| - "source": [ |
| 51 | + "cell_type": "code", |
| 52 | + "collapsed": false, |
| 53 | + "input": [ |
54 | 54 | "def y(t, v0):\n",
|
55 | 55 | " g = 9.81\n",
|
56 | 56 | " return v0*t - 0.5*g*t**2\n",
|
|
60 | 60 | "def y(t):\n",
|
61 | 61 | " g = 9.81\n",
|
62 | 62 | " return v0*t - 0.5*g*t**2"
|
63 |
| - ] |
| 63 | + ], |
| 64 | + "language": "python", |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [] |
64 | 67 | },
|
65 | 68 | {
|
66 | 69 | "cell_type": "markdown",
|
|
298 | 301 | ],
|
299 | 302 | "prompt_number": 4
|
300 | 303 | },
|
| 304 | + { |
| 305 | + "cell_type": "markdown", |
| 306 | + "metadata": {}, |
| 307 | + "source": [ |
| 308 | + "## <span style=\"color:blue\">Exercise 1: Make a function class</span>\n", |
| 309 | + "Make a class called *F* that implements the function\n", |
| 310 | + "\n", |
| 311 | + "$$f(x; a, w) = \\exp(\u2212ax)\\sin(wx).$$\n", |
| 312 | + "\n", |
| 313 | + "A *value(x)* method computes values of *f*, while *a* and *w* are class attributes.\n" |
| 314 | + ] |
| 315 | + }, |
| 316 | + { |
| 317 | + "cell_type": "code", |
| 318 | + "collapsed": false, |
| 319 | + "input": [], |
| 320 | + "language": "python", |
| 321 | + "metadata": {}, |
| 322 | + "outputs": [] |
| 323 | + }, |
| 324 | + { |
| 325 | + "cell_type": "markdown", |
| 326 | + "metadata": {}, |
| 327 | + "source": [ |
| 328 | + "Test the class with the following main program:" |
| 329 | + ] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "code", |
| 333 | + "collapsed": false, |
| 334 | + "input": [ |
| 335 | + "from math import *\n", |
| 336 | + "f = F(a=1.0, w=0.1)\n", |
| 337 | + "print f.value(x=pi)\n", |
| 338 | + "f.a = 2\n", |
| 339 | + "print f.value(pi)" |
| 340 | + ], |
| 341 | + "language": "python", |
| 342 | + "metadata": {}, |
| 343 | + "outputs": [] |
| 344 | + }, |
| 345 | + { |
| 346 | + "cell_type": "markdown", |
| 347 | + "metadata": {}, |
| 348 | + "source": [ |
| 349 | + "## <span style=\"color:blue\">Exercise 2: Make a very simple class</span>\n", |
| 350 | + "Make a class called *Simple* with one attribute *i*, one method *double* which replaces the value of *i* by *i+i*, and a constructor that initializes the attribute. " |
| 351 | + ] |
| 352 | + }, |
| 353 | + { |
| 354 | + "cell_type": "code", |
| 355 | + "collapsed": false, |
| 356 | + "input": [], |
| 357 | + "language": "python", |
| 358 | + "metadata": {}, |
| 359 | + "outputs": [] |
| 360 | + }, |
| 361 | + { |
| 362 | + "cell_type": "markdown", |
| 363 | + "metadata": {}, |
| 364 | + "source": [ |
| 365 | + "Try out the following code for testing the class (but before you run this code, convince yourself what the output of the *print* statements will be):" |
| 366 | + ] |
| 367 | + }, |
| 368 | + { |
| 369 | + "cell_type": "code", |
| 370 | + "collapsed": false, |
| 371 | + "input": [ |
| 372 | + "s1 = Simple(4)\n", |
| 373 | + "for i in range(4):\n", |
| 374 | + " s1.double()\n", |
| 375 | + "print s1.i\n", |
| 376 | + "\n", |
| 377 | + "s2 = Simple('Hello')\n", |
| 378 | + "s2.double(); s2.double()\n", |
| 379 | + "print s2.i\n", |
| 380 | + "s2.i = 100\n", |
| 381 | + "print s2.i" |
| 382 | + ], |
| 383 | + "language": "python", |
| 384 | + "metadata": {}, |
| 385 | + "outputs": [] |
| 386 | + }, |
301 | 387 | {
|
302 | 388 | "cell_type": "markdown",
|
303 | 389 | "metadata": {},
|
|
410 | 496 | "cell_type": "markdown",
|
411 | 497 | "metadata": {},
|
412 | 498 | "source": [
|
413 |
| - "### Protecting attributes\n", |
| 499 | + "## <span style=\"color:blue\">Exercise 3: Extend a class</span>\n", |
| 500 | + "Add an attribute called *transactions* to the *Account* class given above. The new attribute counts the number of transactions done in the *deposit* and *withdraw* methods. The total number of transactions should be printed in the *dump* method. Write a simple test program to demonstrate that transaction gets the right value after some calls to *deposit* and *withdraw*." |
| 501 | + ] |
| 502 | + }, |
| 503 | + { |
| 504 | + "cell_type": "code", |
| 505 | + "collapsed": false, |
| 506 | + "input": [], |
| 507 | + "language": "python", |
| 508 | + "metadata": {}, |
| 509 | + "outputs": [] |
| 510 | + }, |
| 511 | + { |
| 512 | + "cell_type": "markdown", |
| 513 | + "metadata": {}, |
| 514 | + "source": [ |
| 515 | + "## Protecting attributes\n", |
414 | 516 | "It is not possible in Python to explicitly protect attributes from being overwritten by the calling function, *i.e.* the following is possible but not intended:"
|
415 | 517 | ]
|
416 | 518 | },
|
|
663 | 765 | "cell_type": "markdown",
|
664 | 766 | "metadata": {},
|
665 | 767 | "source": [
|
666 |
| - "###Special methods for nice syntax\n", |
| 768 | + "## <span style=\"color:blue\">Exercise 4: Make a class for straight lines</span>\n", |
| 769 | + "Make a class called *Line* whose constructor takes two points $p_1$ and $p_2$ (2-tuples or 2-lists) as input. The line goes through these two points (see function *line* defined below for the relevant formula of the line). A *value(x)* method computes a value on the line at the point *x*." |
| 770 | + ] |
| 771 | + }, |
| 772 | + { |
| 773 | + "cell_type": "code", |
| 774 | + "collapsed": false, |
| 775 | + "input": [ |
| 776 | + "def line(x0, y0, x1, y1):\n", |
| 777 | + " \"\"\"\n", |
| 778 | + " Compute the coefficients a and b in the mathematical\n", |
| 779 | + " expression for a straight line y = a*x + b that goes\n", |
| 780 | + " through two points (x0, y0) and (x1, y1).\n", |
| 781 | + " x0, y0: a point on the line (floats).\n", |
| 782 | + " x1, y1: another point on the line (floats).\n", |
| 783 | + " return: coefficients a, b (floats) for the line (y=a*x+b).\n", |
| 784 | + " \"\"\"\n", |
| 785 | + " a = (y1 - y0)/float(x1 - x0)\n", |
| 786 | + " b = y0 - a*x0\n", |
| 787 | + " return a, b" |
| 788 | + ], |
| 789 | + "language": "python", |
| 790 | + "metadata": {}, |
| 791 | + "outputs": [] |
| 792 | + }, |
| 793 | + { |
| 794 | + "cell_type": "code", |
| 795 | + "collapsed": false, |
| 796 | + "input": [], |
| 797 | + "language": "python", |
| 798 | + "metadata": {}, |
| 799 | + "outputs": [] |
| 800 | + }, |
| 801 | + { |
| 802 | + "cell_type": "markdown", |
| 803 | + "metadata": {}, |
| 804 | + "source": [ |
| 805 | + "## <span style=\"color:blue\">Exercise 5: Make a class for quadratic functions</span>\n", |
| 806 | + "Consider a quadratic function $f(x; a, b, c) = ax^2 + bx + c$. Make a class called *Quadratic* for representing *f*, where *a*, *b*, and *c* are attributes, and the methods are:\n", |
| 807 | + "\n", |
| 808 | + "1. *value* for computing a value of *f* at a point *x*,\n", |
| 809 | + "2. *table* for writing out a table of *x* and *f* values for n *x* values in the\n", |
| 810 | + "interval *[L, R]*,\n", |
| 811 | + "3. *roots* for computing the two roots." |
| 812 | + ] |
| 813 | + }, |
| 814 | + { |
| 815 | + "cell_type": "code", |
| 816 | + "collapsed": false, |
| 817 | + "input": [], |
| 818 | + "language": "python", |
| 819 | + "metadata": {}, |
| 820 | + "outputs": [] |
| 821 | + }, |
| 822 | + { |
| 823 | + "cell_type": "markdown", |
| 824 | + "metadata": {}, |
| 825 | + "source": [ |
| 826 | + "##Special methods for nice syntax\n", |
667 | 827 | "Some class methods have leading and trailing double underscores, *e.g.*,"
|
668 | 828 | ]
|
669 | 829 | },
|
|
861 | 1021 | "a > b # a.__gt__(b)\n",
|
862 | 1022 | "a >= b # a.__ge__(b)"
|
863 | 1023 | ]
|
| 1024 | + }, |
| 1025 | + { |
| 1026 | + "cell_type": "markdown", |
| 1027 | + "metadata": {}, |
| 1028 | + "source": [ |
| 1029 | + "## <span style=\"color:blue\">Exercise 6: A very simple \"Hello, World!\" class</span>\n", |
| 1030 | + "Make a class that can only do one thing: *print a* writes \"Hello, World!\" to the screen, where *a* is an instance of the class." |
| 1031 | + ] |
| 1032 | + }, |
| 1033 | + { |
| 1034 | + "cell_type": "code", |
| 1035 | + "collapsed": false, |
| 1036 | + "input": [], |
| 1037 | + "language": "python", |
| 1038 | + "metadata": {}, |
| 1039 | + "outputs": [] |
| 1040 | + }, |
| 1041 | + { |
| 1042 | + "cell_type": "markdown", |
| 1043 | + "metadata": {}, |
| 1044 | + "source": [ |
| 1045 | + "## <span style=\"color:blue\">Exercise 7: Use special methods</span>\n", |
| 1046 | + "Modify the class from the first exercise such that the following code works:" |
| 1047 | + ] |
| 1048 | + }, |
| 1049 | + { |
| 1050 | + "cell_type": "code", |
| 1051 | + "collapsed": false, |
| 1052 | + "input": [ |
| 1053 | + "f = F2(1.0, 0.1)\n", |
| 1054 | + "print f(pi)\n", |
| 1055 | + "f.a = 2\n", |
| 1056 | + "print f(pi)\n", |
| 1057 | + "print f" |
| 1058 | + ], |
| 1059 | + "language": "python", |
| 1060 | + "metadata": {}, |
| 1061 | + "outputs": [] |
864 | 1062 | }
|
865 | 1063 | ],
|
866 | 1064 | "metadata": {}
|
|
0 commit comments