File size: 12,458 Bytes
532ef6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Object Oriented Programming in Python\n",
    "\n",
    "I won't explain here why OOP is useful, but I will go over the basics of what it is.\n",
    "\n",
    "When we talk about OOP we're talking about about *classes* and *objects*\n",
    "\n",
    "OOP in python is not that different than OOP in any other language.\n",
    "\n",
    "## What is a class?\n",
    "\n",
    "a *class* is a user defined data type, where we give a name to the data type\n",
    "\n",
    "int is the name of a data type, float is the name of a data type, list is the name of a data type\n",
    "\n",
    "Similarly, if I define a class called My_data_type then My_data_type is the name of a data type.\n",
    "\n",
    "This data type can contain many variables inside of it with each variariable possibly being of different data types, and each variable can have it's own name.  At first glance a class seems a lot like a dictionary.  We will soon see how it's different.\n",
    "\n",
    "## Attributes\n",
    "\n",
    "A variable inside a class is called an *attribute.*\n",
    "\n",
    "## Methods\n",
    "\n",
    "How is this different than a list or dictionary?\n",
    "\n",
    "You can also have functions inside of a class.  You call a function that is inside of a class a *method*\n",
    "\n",
    "These functions can perform calculations on the attributes or on data fed to them from outside of the class.\n",
    "\n",
    "In the declaration of a class you can only define methods.  If you want to declare an attribute this must be done inside a method.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## Objects\n",
    "\n",
    "An *object* is an instance of a particular class.\n",
    "\n",
    "For example, one existing data type in python is int\n",
    "\n",
    "If I define a variable as \n",
    "first_int = 6\n",
    "\n",
    "Then first_int is an object of the type int\n",
    "\n",
    "I can also define\n",
    "second_int = 532\n",
    "\n",
    "This is another instance of the type int\n",
    "\n",
    "These two instances of the type int have different values assigned to them!\n",
    "\n",
    "Similarly I can define my own type of variable as a class called My_data_type, then I can declare multiple objects of the class My_data_type, each having different names and possibly having different values assigned to their attributes and different execution of the methods.  We have to name the data type (int), but we also have to name each instance of the data type (first_int, second_int). \n",
    "\n",
    "This will all become clear when we do an example.\n",
    "\n",
    "## Initializer\n",
    "\n",
    "If you want to assign some attribute values to your object when you declare it we use an *initializer* in the class definition.  We can also automatically run other methods from inside the initializer at the time of declaration.\n",
    "\n",
    "## self\n",
    "\n",
    "self is an important concept for classes.  self is python's internal reference identifier for classes. It serves 2 main purposes.\n",
    "\n",
    "1) When you add an attribute (variable) in a class you must name it self.attribute_name.  Then when you declare an object of that class you access that attribute from the outside as object_name.attribute_name, or if you want to use it interally in a method you would reference it as self.attribute_name.\n",
    "\n",
    "2) When you add a method (function) to a class, you *must* have self as the first argument of that function. but when you call a function you don't actually input self... this is a little confusing.\n",
    "\n",
    "### Let's look at an example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.7\n",
      "3.74\n"
     ]
    }
   ],
   "source": [
    "# the name of the data type (class) must always start with a capital letter\n",
    "\n",
    "class My_data_type:  # first letter capitalized\n",
    "    def init_some_vals(self,val2):  # self is first argument of function\n",
    "        self.first_var = 1.7        # attributes named self.attribute_name\n",
    "        self.second_var = val2      # this function doesn't return anything...it just sets some attribute values\n",
    "    def multiply_vals(self):        # another internal method, but this one does return a value\n",
    "        return self.first_var*self.second_var\n",
    "    \n",
    "me = My_data_type()    # declare an object of the class My_data_type\n",
    "me.init_some_vals(2.2) # don't give self as an input!!! \n",
    "print(me.first_var)    # access attribute\n",
    "print(me.multiply_vals()) # run a method\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.74"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1.7*2.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'self' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-a79240bde32d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfirst_var\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# self is only used inside the class definition!!!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'self' is not defined"
     ]
    }
   ],
   "source": [
    "print(self.first_var)  # self is only used inside the class definition!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "231.3\n"
     ]
    }
   ],
   "source": [
    "# we can also add attributes to an object outside of the class definition\n",
    "\n",
    "me.third_var = 231.3\n",
    "print(me.third_var)\n",
    "\n",
    "# this is generally a bad practice though\n",
    "# we want each object of the same class to have the same attributes (not values of attributes)\n",
    "# so that this is internally consistent\n",
    "# in many languages it isn't possible to add attributes from outside the class definition for this very reason"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'My_data_type' object has no attribute 'third_var'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-ab825305da75>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0myou\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mMy_data_type\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0myou\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minit_some_vals\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m6.1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0myou\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mthird_var\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'My_data_type' object has no attribute 'third_var'"
     ]
    }
   ],
   "source": [
    "you = My_data_type()\n",
    "you.init_some_vals(6.1)\n",
    "print(you.third_var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if we don't want to have to call the initialization function with can use an initalizer with __init__\n",
    "# everything inside __init__ will automatically be run when an object is declared\n",
    "class Pet:\n",
    "    def __init__(self,animal_type,name,age,weight_lbs,color):  # initializer\n",
    "        self.animal = animal_type    # assign some attribute values from the input arguments of the initializer\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight_lbs = weight_lbs\n",
    "        self.color = color\n",
    "        self.weight_kg = self.calc_weight_in_kg()  # automatically run a method...still don't feed self to the call\n",
    "    \n",
    "    def calc_weight_in_kg(self):  # remember to give self as an input...even though we don't use it when we call\n",
    "        return 0.453592*self.weight_lbs\n",
    "        \n",
    "    def describe_pet(self):\n",
    "        print('This pet is a',self.color, self.animal,)\n",
    "        print('This pet\\'s name is',self.name,'and it is',self.age,'years old')\n",
    "        print('This pet weighs',self.weight_lbs,'pounds, which is',round(self.weight_kg,2),'kilograms')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This pet is a orange cat\n",
      "This pet's name is Mittens and it is 3 years old\n",
      "This pet weighs 7 pounds, which is 3.18 kilograms\n",
      "\n",
      "This pet is a brown dog\n",
      "This pet's name is Spot and it is 9 years old\n",
      "This pet weighs 18 pounds, which is 8.16 kilograms\n"
     ]
    }
   ],
   "source": [
    "# now create 2 objects of the class Pet\n",
    "my_cat = Pet('cat','Mittens',3,7,'orange')\n",
    "mydog = Pet('dog','Spot',9,18,'brown')\n",
    "my_cat.describe_pet()\n",
    "print('')  # just so there's some space between the object outputs\n",
    "mydog.describe_pet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "orange\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# we can also access the attributes of the class from outside because they were declared using self!\n",
    "print(my_cat.color)\n",
    "print(mydog.age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "3.175144\n"
     ]
    }
   ],
   "source": [
    "# I can even change the value of attributes from outside\n",
    "my_cat.weight_lbs = 5\n",
    "print(my_cat.weight_lbs)\n",
    "print(my_cat.weight_kg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.26796\n",
      "8.164656\n",
      "3.175144\n",
      "2.26796\n"
     ]
    }
   ],
   "source": [
    "# we can also access methods from outside\n",
    "print(my_cat.calc_weight_in_kg())\n",
    "print(mydog.calc_weight_in_kg())\n",
    "print(my_cat.weight_kg)\n",
    "my_cat.weight_kg=my_cat.calc_weight_in_kg()\n",
    "print(my_cat.weight_kg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# notice that these 2 method calls returned different values because they are parts of different objects!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}