|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# [Classes](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes)" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "class MyFirstClass:\n", |
| 17 | + " def __init__(self, name):\n", |
| 18 | + " self.name = name\n", |
| 19 | + "\n", |
| 20 | + " def greet(self):\n", |
| 21 | + " print('Hello {}!'.format(self.name))" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "my_instance = MyFirstClass('John Doe')\n", |
| 31 | + "print('my_intance: {}'.format(my_instance))\n", |
| 32 | + "print('type: {}'.format(type(my_instance)))\n", |
| 33 | + "print('my_instance.name: {}'.format(my_instance.name))" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "## Methods\n", |
| 41 | + "The functions inside classes are called methods. They are used similarly as functions. " |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": null, |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "alice = MyFirstClass(name='Alice')\n", |
| 51 | + "alice.greet()" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "### `__init__()`\n", |
| 59 | + "`__init__()` is a special method that is used for initialising instances of the class. It's called when you create an instance of the class. " |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "class Example:\n", |
| 69 | + " def __init__(self):\n", |
| 70 | + " print('Now we are inside __init__')\n", |
| 71 | + " \n", |
| 72 | + "print('creating instance of Example')\n", |
| 73 | + "example = Example()\n", |
| 74 | + "print('instance created')" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "`__init__()` is typically used for initialising instance variables of your class. These can be listed as arguments after `self`. To be able to access these instance variables later during your instance's lifetime, you have to save them into `self`. `self` is the first argument of the methods of your class and it's your access to the instance variables and other methods. " |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "class Example:\n", |
| 91 | + " def __init__(self, var1, var2):\n", |
| 92 | + " self.first_var = var1\n", |
| 93 | + " self.second_var = var2\n", |
| 94 | + " \n", |
| 95 | + " def print_variables(self):\n", |
| 96 | + " print('{} {}'.format(self.first_var, self.second_var))\n", |
| 97 | + " \n", |
| 98 | + "e = Example('abc', 123)\n", |
| 99 | + "e.print_variables()\n", |
| 100 | + " " |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "## Class variables vs instance variables\n", |
| 108 | + "Class variables are shared between all the instances of that class whereas instance variables can hold different values between different instances of that class." |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": null, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [], |
| 116 | + "source": [ |
| 117 | + "class Example:\n", |
| 118 | + " # These are class variables\n", |
| 119 | + " name = 'Example class'\n", |
| 120 | + " description = 'Just an example of a simple class'\n", |
| 121 | + "\n", |
| 122 | + " def __init__(self, var1):\n", |
| 123 | + " # This is an instance variable\n", |
| 124 | + " self.instance_variable = var1\n", |
| 125 | + "\n", |
| 126 | + " def show_info(self):\n", |
| 127 | + " info = 'instance_variable: {}, name: {}, description: {}'.format(\n", |
| 128 | + " self.instance_variable, Example.name, Example.description)\n", |
| 129 | + " print(info)\n", |
| 130 | + "\n", |
| 131 | + "\n", |
| 132 | + "inst1 = Example('foo')\n", |
| 133 | + "inst2 = Example('bar')\n", |
| 134 | + "\n", |
| 135 | + "# name and description have identical values between instances\n", |
| 136 | + "assert inst1.name == inst2.name == Example.name\n", |
| 137 | + "assert inst1.description == inst2.description == Example.description\n", |
| 138 | + "\n", |
| 139 | + "# If you change the value of a class variable, it's changed across all instances\n", |
| 140 | + "Example.name = 'Modified name'\n", |
| 141 | + "inst1.show_info()\n", |
| 142 | + "inst2.show_info()" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "metadata": { |
| 147 | + "kernelspec": { |
| 148 | + "display_name": "Python 3", |
| 149 | + "language": "python", |
| 150 | + "name": "python3" |
| 151 | + }, |
| 152 | + "language_info": { |
| 153 | + "codemirror_mode": { |
| 154 | + "name": "ipython", |
| 155 | + "version": 3 |
| 156 | + }, |
| 157 | + "file_extension": ".py", |
| 158 | + "mimetype": "text/x-python", |
| 159 | + "name": "python", |
| 160 | + "nbconvert_exporter": "python", |
| 161 | + "pygments_lexer": "ipython3", |
| 162 | + "version": "3.5.4" |
| 163 | + } |
| 164 | + }, |
| 165 | + "nbformat": 4, |
| 166 | + "nbformat_minor": 2 |
| 167 | +} |
0 commit comments