1
00:00:00,000 --> 00:00:03,990
Classes in Python are how you make objects.

2
00:00:04,000 --> 00:00:09,990
The classes themselves are the blueprint for how an object is created.

3
00:00:10,000 --> 00:00:11,990
For example, here is a class called Duck.

4
00:00:12,000 --> 00:00:16,990
And it implements a couple of methods called quack and walk, so that when you

5
00:00:17,000 --> 00:00:21,990
create a duck object, that duck will quack like a duck and walk like a duck and

6
00:00:22,000 --> 00:00:23,990
that of course will make it a duck.

7
00:00:24,000 --> 00:00:26,990
An object is an instance of a class.

8
00:00:27,000 --> 00:00:30,990
That means that when you create an object, it's a separate thing.

9
00:00:31,000 --> 00:00:32,990
It's separately encapsulated.

10
00:00:33,000 --> 00:00:34,990
It has all of its own attributes.

11
00:00:35,000 --> 00:00:39,990
So if you make several different objects from a given class, each of those

12
00:00:40,000 --> 00:00:43,990
different instance of the class, but they're all separately encapsulated and

13
00:00:44,000 --> 00:00:48,990
have their own data space, their own code space and are essentially their own objects.

14
00:00:49,000 --> 00:00:50,990
An object is an instance of a class.

15
00:00:51,000 --> 00:00:55,990
That means when you create an object, that object is built from the blueprint of

16
00:00:56,000 --> 00:00:57,990
the class, but it's its own object.

17
00:00:58,000 --> 00:00:59,990
Its own encapsulation.

18
00:01:00,000 --> 00:01:02,990
And so if you create several different objects from the same class,

19
00:01:03,000 --> 00:01:03,990
they're separately encapsulated.

20
00:01:04,000 --> 00:01:07,990
They have their own data space and they have their own attributes, and they

21
00:01:08,000 --> 00:01:09,990
operate independently of one another.

22
00:01:10,000 --> 00:01:13,990
So in this case, we've created an object called donald from the Duck class.

23
00:01:14,000 --> 00:01:16,990
And we can say donald.quack and donald.walk.

24
00:01:17,000 --> 00:01:22,990
And donald will quack like a duck and will walk like a duck and therefore is a duck.

25
00:01:23,000 --> 00:01:24,990
Let's take a look at how this works in practice.

26
00:01:25,000 --> 00:01:28,990
So we'll start by making a working copy of classes.py, and we'll call

27
00:01:29,000 --> 00:01:33,990
it classes-working.py.

28
00:01:34,000 --> 00:01:36,990
Go ahead and open that working copy.

29
00:01:37,000 --> 00:01:40,990
And we see we have our Duck class and we have donald.

30
00:01:41,000 --> 00:01:45,990
So if we go ahead and run this, you'll see that donald quacks like a duck and

31
00:01:46,000 --> 00:01:47,990
donald walks like a duck.

32
00:01:48,000 --> 00:01:50,990
So let's take a look at how this works.

33
00:01:51,000 --> 00:01:54,990
Class is the keyword that introduces the definition of a class.

34
00:01:55,000 --> 00:01:58,990
Duck here is the name of the class and then there is a colon, and so because

35
00:01:59,000 --> 00:02:02,990
there is a colon, you have things that are indented under this level.

36
00:02:03,000 --> 00:02:06,990
And those are the suite of the class definition.

37
00:02:07,000 --> 00:02:09,990
And in this case we have two things inside the class.

38
00:02:10,000 --> 00:02:16,990
We have a function called quack and that function, because it has self as its

39
00:02:17,000 --> 00:02:21,990
first argument, is actually a method of the class.

40
00:02:22,000 --> 00:02:23,990
And we'll talk in a moment about how this works.

41
00:02:24,000 --> 00:02:28,990
Then we have walk, which is defined in the same way, and so that's another method.

42
00:02:29,000 --> 00:02:33,990
And then down here, when we create the object donald, we create the object

43
00:02:34,000 --> 00:02:36,990
donald by assigning it from Duck.

44
00:02:37,000 --> 00:02:39,990
And Duck is the class definition.

45
00:02:40,000 --> 00:02:42,990
And so now donald is an object.

46
00:02:43,000 --> 00:02:46,990
And donald is an object of the class Duck.

47
00:02:47,000 --> 00:02:52,990
So if I were to just go here and I were to say print(donald), like that, save

48
00:02:53,000 --> 00:02:57,990
that and run it, then it says <__main__.Duck object at.

49
00:02:58,000 --> 00:03:03,990
And so donald is an object of the class Duck.

50
00:03:04,000 --> 00:03:08,990
So when I call the quack method, I use this dot operator and that dot operator

51
00:03:09,000 --> 00:03:10,990
is the attribute dereference operator.

52
00:03:11,000 --> 00:03:14,990
That means that it's going to look inside the object donald for an

53
00:03:15,000 --> 00:03:15,990
attribute called quack.

54
00:03:16,000 --> 00:03:19,990
And because it has these parentheses on it, it's going to go ahead and call it

55
00:03:20,000 --> 00:03:22,990
as an object method.

56
00:03:23,000 --> 00:03:27,990
And so that will call this code, and it prints this quack, and donald.walk calls

57
00:03:28,000 --> 00:03:31,990
this code, and it prints that "Walks like a duck."

58
00:03:32,000 --> 00:03:38,990
So Duck is the class and that's all this stuff here, and donald is the object.

59
00:03:39,000 --> 00:03:41,990
He's an instance of Duck.

60
00:03:42,000 --> 00:03:45,990
And you can call the methods inside donald.

61
00:03:46,000 --> 00:03:49,990
And so you can call the methods inside the donald object of class Duck

62
00:03:50,000 --> 00:03:51,990
using this dot notation.

63
00:03:52,000 --> 00:03:56,990
So that's how you define a class and create objects based on it in Python.

64
00:03:57,000 --> 00:03:59,990
Python is fundamentally an object-oriented language.

65
00:04:00,000 --> 00:04:01,990
And that means you're going to do this a lot.

66
00:04:02,000 --> 00:04:04,990
There are times when you just have a single thing that you want to do and you

67
00:04:05,000 --> 00:04:08,990
want to do it in a very simple way.

68
00:04:09,000 --> 00:04:12,990
And for that, you'll just create a function and you'll use a function.

69
00:04:13,000 --> 00:04:15,990
Often you're going to want to do things that are more complicated and these

70
00:04:16,000 --> 00:04:18,990
more complicated things are going to have their own local data and they're

71
00:04:19,000 --> 00:04:24,990
going to have multiple methods that are tightly related to each other and

72
00:04:25,000 --> 00:04:25,990
interact with each other.

73
00:04:26,000 --> 00:04:30,990
And these are the times when we're going to go ahead and create a class and use objects.

74
00:04:31,000 --> 00:04:34,990
As you go through the process of writing programs in Python, especially if

75
00:04:35,000 --> 00:04:38,990
you've not worked with an object- oriented language before, you're going to see

76
00:04:39,000 --> 00:04:43,990
more and more cases where objects make sense.

77
00:04:44,000 --> 00:04:46,990
If you're used to using functions a lot, you'll be creating a lot of functions

78
00:04:47,000 --> 00:04:49,990
for things, but over time as you get more familiar with the power and the

79
00:04:50,000 --> 00:04:53,990
flexibility and the encapsulation and inheritance of the different properties of

80
00:04:54,000 --> 00:04:56,990
objects that we're going to go through in this chapter, you're going to use

81
00:04:57,000 --> 00:04:57,990
objects more and more.

82
00:04:58,000 --> 00:05:01,990
And you'll end up using objects for a lot of the things that you used to use

83
00:05:02,000 --> 00:05:02,990
multiple functions for.

84
00:05:03,000 --> 00:05:04,990
So objects are very powerful.

85
00:05:05,000 --> 00:05:08,990
Python is a fundamentally object- oriented language, so you're going to use these

86
00:05:09,000 --> 00:05:19,000
techniques often as you write code in Python.

