1
00:00:00,000 --> 00:00:03,990
When you're creating objects and classes in Python, you're going to want to get

2
00:00:04,000 --> 00:00:05,990
them some functionality.

3
00:00:06,000 --> 00:00:08,990
That functionality is usually created with methods.

4
00:00:09,000 --> 00:00:12,990
So let's go ahead and make a working copy of classes.py.

5
00:00:13,000 --> 00:00:17,990
We'll call this classes-working.py.

6
00:00:18,000 --> 00:00:19,990
We'll open up that working copy.

7
00:00:20,000 --> 00:00:23,990
You can see that we have a class called Duck and it already has couple of

8
00:00:24,000 --> 00:00:25,990
methods in it, quack and walk.

9
00:00:26,000 --> 00:00:27,990
Those get called down here.

10
00:00:28,000 --> 00:00:29,990
So donald gets created as class Duck.

11
00:00:30,000 --> 00:00:33,990
So this is an object with that class and the object donald.

12
00:00:34,000 --> 00:00:37,990
We're calling the quack method on the object donald and we're calling the walk

13
00:00:38,000 --> 00:00:39,990
method on the object donald.

14
00:00:40,000 --> 00:00:45,990
The calls are done using this dot operator, which is used to reference an

15
00:00:46,000 --> 00:00:48,990
attribute of the object.

16
00:00:49,000 --> 00:00:50,990
In this case, the attribute is a method.

17
00:00:51,000 --> 00:00:56,990
So it's called with the parenthesis and that calls it as you would call a function.

18
00:00:57,000 --> 00:00:57,990
These are actually functions.

19
00:00:58,000 --> 00:01:01,990
It's just that these are functions that are attributes of an object.

20
00:01:02,000 --> 00:01:03,990
That's what makes the methods.

21
00:01:04,000 --> 00:01:07,990
So we'll go ahead and we'll run this, and you'll see that the object gets

22
00:01:08,000 --> 00:01:09,990
created and the methods get called.

23
00:01:10,000 --> 00:01:10,990
It prints out Quaaack!

24
00:01:11,000 --> 00:01:12,990
and it prints out Walks like a duck.

25
00:01:13,000 --> 00:01:14,990
Everything is working like we expected it to.

26
00:01:15,000 --> 00:01:17,990
So methods are actually functions.

27
00:01:18,000 --> 00:01:20,990
You'll see that they're defined just like you define functions in Python.

28
00:01:21,000 --> 00:01:25,990
The difference is these are indented under the class Duck.

29
00:01:26,000 --> 00:01:28,990
So that makes them methods of the class Duck.

30
00:01:29,000 --> 00:01:33,990
You'll also notice that the first argument to these functions is this word self.

31
00:01:34,000 --> 00:01:38,990
That is a reference to the object. Not the class, but the object.

32
00:01:39,000 --> 00:01:43,990
In other words, when quack gets called on the donald object, then the donald

33
00:01:44,000 --> 00:01:47,990
object gets passed to the quack method.

34
00:01:48,000 --> 00:01:50,990
So the quack has a way of referring to object attributes.

35
00:01:51,000 --> 00:01:55,990
You'll also notice that nothing is getting passed inside of these parentheses.

36
00:01:56,000 --> 00:02:00,990
It's as if donald was passed inside those parentheses, but you don't

37
00:02:01,000 --> 00:02:01,990
actually put that there.

38
00:02:02,000 --> 00:02:05,990
That happens automatically by virtue of the dot operator.

39
00:02:06,000 --> 00:02:12,990
When quack gets called as a method of donald, then donald gets passed magically

40
00:02:13,000 --> 00:02:18,990
inside those parentheses, so that it's the first argument to the method.

41
00:02:19,000 --> 00:02:22,990
There's a special type of a method that I want to talk about which is called a constructor.

42
00:02:23,000 --> 00:02:27,990
This gets called every time you create an object based on this class.

43
00:02:28,000 --> 00:02:32,990
The constructor in Python is created by naming a method with two underscores and

44
00:02:33,000 --> 00:02:35,990
the word init and two more underscores at the end.

45
00:02:36,000 --> 00:02:37,990
This creates a constructor method.

46
00:02:38,000 --> 00:02:41,990
So I'm just going to print('constructor') here.

47
00:02:42,000 --> 00:02:46,990
And we'll go ahead and we'll save this and we'll run it.

48
00:02:47,000 --> 00:02:49,990
You'll see that constructor gets printed at the beginning.

49
00:02:50,000 --> 00:02:54,990
So when we run this, the first thing we do is we create the donald object and

50
00:02:55,000 --> 00:02:57,990
that's where the constructor gets called.

51
00:02:58,000 --> 00:03:04,990
In fact, one of the most common purposes for a constructor is to initialize some data.

52
00:03:05,000 --> 00:03:09,990
So if I put in the number 47 here and then I pass in that value in the

53
00:03:10,000 --> 00:03:15,990
constructor, I'll say value here, instead of printing constructor here, I can

54
00:03:16,000 --> 00:03:21,990
save this value, saying self._v = value.

55
00:03:22,000 --> 00:03:27,990
What this does is this creates a local variable that is an attribute of the object.

56
00:03:28,000 --> 00:03:31,990
In this case, the donald object, but if I had different objects with different names,

57
00:03:32,000 --> 00:03:34,990
it would be attributes of those objects.

58
00:03:35,000 --> 00:03:37,990
We'll see how valuable this is in a moment.

59
00:03:38,000 --> 00:03:41,990
Then I can use that value in the methods.

60
00:03:42,000 --> 00:03:44,990
For example, I can print it out here.

61
00:03:45,000 --> 00:03:48,990
I can print it out here.

62
00:03:49,000 --> 00:03:53,990
So now I'm initializing this variable to the number 47.

63
00:03:54,000 --> 00:03:57,990
Each time I use it, I'm going to go ahead and print it out.

64
00:03:58,000 --> 00:03:59,990
So we'll save this and run it.

65
00:04:00,000 --> 00:04:03,990
Now we see that we have a 47 here and a 47 there.

66
00:04:04,000 --> 00:04:07,990
If I were to change this to something else, save it and run it.

67
00:04:08,000 --> 00:04:09,990
We have 52 here and 52 there.

68
00:04:10,000 --> 00:04:20,990
If I create a different object with a different number in it and go ahead and

69
00:04:21,000 --> 00:04:29,990
call it, now when I save that and run it, then we have the donald ducks and we

70
00:04:30,000 --> 00:04:30,990
have the frank ducks.

71
00:04:31,000 --> 00:04:38,990
So the value here is that this self._v is actually attached to the object, not to the class.

72
00:04:39,000 --> 00:04:44,990
This is what's called encapsulation in object oriented programming, because that

73
00:04:45,000 --> 00:04:47,990
value is a part of the object.

74
00:04:48,000 --> 00:04:51,990
So I can have different objects with different values.

75
00:04:52,000 --> 00:04:54,990
I could actually have all kinds of different data associated with those objects.

76
00:04:55,000 --> 00:04:58,990
They could be opening different databases, they could be accessing different

77
00:04:59,000 --> 00:05:02,990
files, they could be keeping their place in these databases and in these files.

78
00:05:03,000 --> 00:05:06,990
This is the power of encapsulation in object oriented programming.

79
00:05:07,000 --> 00:05:09,990
We'll see a lot of examples of that as we move forward, but for now what's

80
00:05:10,000 --> 00:05:15,990
important to know is that all this happens to this self variable right there and right there.

81
00:05:16,000 --> 00:05:20,990
When the object calls a method that self variable gets passed, and that's a

82
00:05:21,000 --> 00:05:22,990
reference to the object.

83
00:05:23,000 --> 00:05:25,990
All the things that are attached to the object, its other methods, its

84
00:05:26,000 --> 00:05:28,990
attributes, its data, is all carried there.

85
00:05:29,000 --> 00:05:31,990
So here we've defined methods in our object.

86
00:05:32,000 --> 00:05:36,990
These methods are defined in exactly the same way that we define a function in

87
00:05:37,000 --> 00:05:40,990
Python except that its first argument is always self and that argument doesn't

88
00:05:41,000 --> 00:05:42,990
get passed explicitly.

89
00:05:43,000 --> 00:05:45,990
It's passed implicitly through the dot operator.

90
00:05:46,000 --> 00:05:49,990
Then we have a special method called init which is used as a constructor.

91
00:05:50,000 --> 00:05:51,990
We can use that to set up data.

92
00:05:52,000 --> 00:05:53,990
We can use it to open databases.

93
00:05:54,000 --> 00:05:56,990
We can use it for all kinds of purposes.

94
00:05:57,000 --> 00:06:00,990
The point of it is to initialize whatever needs to get initialized when you

95
00:06:01,000 --> 00:06:03,990
create the object based on this class.

96
00:06:04,000 --> 00:06:06,990
So that's how you can create a constructor and that's how you create methods in

97
00:06:07,000 --> 00:06:17,000
classes that will be used in objects in Python.

