Skip to content

Commit 6632ea9

Browse files
committed
W5: Person constructor
1 parent 7dcc59f commit 6632ea9

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

week5/w5.ipynb

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,81 @@
169169
{
170170
"cell_type": "markdown",
171171
"metadata": {},
172-
"source": []
172+
"source": [
173+
"We can now create an *instance* of Person with the same notation used to call functions!\n",
174+
"`my_person` is an instance of `Person`."
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"my_person = Person()"
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"metadata": {},
189+
"source": [
190+
"Great, we've defined a Person class, but this doesn't really do much right now.\n",
191+
"What characteristics might we want to store about a person? These will determine what the attributes our objects will have.\n",
192+
"To keep things simple, let's consider a person's name, age, and profession.\n",
193+
"\n",
194+
"We need to update our \"blueprint\", the `Person` class, so that instances of this class contain these attributes!\n",
195+
"This is done using a constructor. The constructor is a special method used to define how instances of the `Person` class\n",
196+
"should be initialized. When we instantiate a new instance of `Person` with `my_person = Person()`, Python will attempt to invoke\n",
197+
"a method called `__init__` if it exists.\n",
198+
"\n",
199+
"In order to define a constructor, we define a method, more precisely an instance method, with the name `__init__`\n",
200+
"in our class (**important:** note the indentation of the method).\n",
201+
"\n",
202+
"Instance methods are \"attached\" to an object and they can refer to other methods and attributes of the object.\n",
203+
"To that end, we need to be able to refer to the object which owns the instance method.\n",
204+
"This is done by adding a `self` parameter to **every** instance method. This parameter **must** also be the first\n",
205+
"parameter of every instance method.\n",
206+
"\n",
207+
"Below we have added a constructor to the `Person` class. It takes `self` (the object which was just created), the name of the\n",
208+
"person, age, and profession as parameters and then we \"store\" these in the new object. We can access and set the attributes\n",
209+
"of an object using a `.` followed by the attribute name as shown below.\n",
210+
"\n",
211+
"You may have also noticed that this method starts and ends with `__`. \n",
212+
"There are other methods named in a similar way.\n",
213+
"These methods are often called *magic methods* and used to defined very specific behaviours (If you're curious you can find out more [here](https://docs.python.org/3/reference/datamodel.html#special-method-names))."
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"metadata": {},
220+
"outputs": [],
221+
"source": [
222+
"class Person:\n",
223+
" def __init__(self, name, age, profession):\n",
224+
" self.name = name\n",
225+
" self.age = age\n",
226+
" self.profession = profession"
227+
]
228+
},
229+
{
230+
"cell_type": "markdown",
231+
"metadata": {},
232+
"source": [
233+
"Now we can instantiate a person like so."
234+
]
235+
},
236+
{
237+
"cell_type": "code",
238+
"execution_count": null,
239+
"metadata": {},
240+
"outputs": [],
241+
"source": [
242+
"my_person = Person(\"tomas\", 20, \"computer scientist\")\n",
243+
"\n",
244+
"print(my_person.name)\n",
245+
"print(my_person.age)"
246+
]
173247
},
174248
{
175249
"cell_type": "markdown",

0 commit comments

Comments
 (0)