1
00:00:00,000 --> 00:00:02,990
Python is fundamentally an object-oriented language.

2
00:00:03,000 --> 00:00:07,990
In fact, in Python 3 everything is an object.

3
00:00:08,000 --> 00:00:10,990
So let's look at how we can create our own objects.

4
00:00:11,000 --> 00:00:19,990
We'll start by making a working copy of syntax.py. I'll call this syntax-objects.py.

5
00:00:20,000 --> 00:00:21,990
And we'll open our working copy.

6
00:00:22,000 --> 00:00:24,990
Now we'll go into a lot more detail on objects later on.

7
00:00:25,000 --> 00:00:29,990
The point here is just to get a handle on the syntax of creating objects.

8
00:00:30,000 --> 00:00:33,990
So we'll go ahead and start by defining a class.

9
00:00:34,000 --> 00:00:38,990
Now a class is the definition that's used when creating an object in Python.

10
00:00:39,000 --> 00:00:44,990
So we'll use the class keyword, and we'll call this class Egg.

11
00:00:45,000 --> 00:00:49,990
And the first thing we'll define inside of the class is a constructor.

12
00:00:50,000 --> 00:00:53,990
And a constructor in Python is a method, or a function inside the class, and has a

13
00:00:54,000 --> 00:00:59,990
special name which is __init__, like that.

14
00:01:00,000 --> 00:01:04,990
And all methods within classes have self as the first argument.

15
00:01:05,000 --> 00:01:07,990
This is a reference to the object itself.

16
00:01:08,000 --> 00:01:09,990
And it's traditionally called self.

17
00:01:10,000 --> 00:01:13,990
It's not required, but it's a really, really good idea to always call it self.

18
00:01:14,000 --> 00:01:18,990
And following self can be arguments that can be used in constructing the object.

19
00:01:19,000 --> 00:01:21,990
And in this case we are going to say how do you want your eggs?

20
00:01:22,000 --> 00:01:25,990
And so it'll be a kind of eggs, and we'll call it kind, and we'll give it a

21
00:01:26,000 --> 00:01:30,990
default value of fried.

22
00:01:31,000 --> 00:01:34,990
And then we will assign this value to an object variable.

23
00:01:35,000 --> 00:01:42,990
And object variables are referenced to the object itself, so we'll say self.kind

24
00:01:43,000 --> 00:01:47,990
= kind, and that will create an object variable that will be carried around and

25
00:01:48,000 --> 00:01:49,990
encapsulated in the object.

26
00:01:50,000 --> 00:01:56,990
And so each instance of this class, each object, can have a different value for kind.

27
00:01:57,000 --> 00:02:01,990
Now let's define a method for finding out what kind.

28
00:02:02,000 --> 00:02:09,990
And we'll call this whatKind, and it has self as the only argument.

29
00:02:10,000 --> 00:02:13,990
And we'll simply return self.kind.

30
00:02:14,000 --> 00:02:15,990
Now we have a class.

31
00:02:16,000 --> 00:02:18,990
It's a very simple class, and it doesn't do anything terribly useful, but it's

32
00:02:19,000 --> 00:02:23,990
good as an example of the syntax of how you create a class.

33
00:02:24,000 --> 00:02:25,990
And now we'll go ahead and create an object.

34
00:02:26,000 --> 00:02:27,990
Now an object is an instance of a class.

35
00:02:28,000 --> 00:02:30,990
You can think of the class as like the blueprint.

36
00:02:31,000 --> 00:02:36,990
This defines how the object is created, and the object itself is an instance.

37
00:02:37,000 --> 00:02:41,990
The object is an encapsulation of all of the methods and the variables that are

38
00:02:42,000 --> 00:02:43,990
inside of the class.

39
00:02:44,000 --> 00:02:50,990
And so we'll say fried = Egg, like that.

40
00:02:51,000 --> 00:02:56,990
And that will create a object called fried, based on the class, Egg.

41
00:02:57,000 --> 00:02:59,990
And you notice we don't have anything in the parentheses, so it will use this

42
00:03:00,000 --> 00:03:04,990
default value when it creates or constructs the object.

43
00:03:05,000 --> 00:03:08,990
The constructor is called every time you create an object.

44
00:03:09,000 --> 00:03:11,990
So here we've created an object with the assignment operator.

45
00:03:12,000 --> 00:03:15,990
And so the first thing it'll do is it'll call this Constructor, and it'll

46
00:03:16,000 --> 00:03:16,990
assign this default value.

47
00:03:17,000 --> 00:03:28,990
We can also create a scrambled egg, like this.

48
00:03:29,000 --> 00:03:39,990
And so when we print these out, later on we can say print(fried.whatKind()).

49
00:03:40,000 --> 00:03:45,990
And if we save this and run it, it says that it's a fried egg.

50
00:03:46,000 --> 00:03:51,990
Instead of the fried kind, I used the scrambled kind, and this is just this different

51
00:03:52,000 --> 00:03:55,990
object, but it's using exactly the same interface.

52
00:03:56,000 --> 00:03:57,990
If I save it and run it,

53
00:03:58,000 --> 00:03:59,990
now we have a scrambled egg.

54
00:04:00,000 --> 00:04:06,990
So this is just a very simple and a very simplistic example of how an object is created.

55
00:04:07,000 --> 00:04:12,990
We have a definition, which is called a class, and this defines the blueprint of

56
00:04:13,000 --> 00:04:13,990
how these objects are made.

57
00:04:14,000 --> 00:04:20,990
And then we have a couple of objects that are created using that class.

58
00:04:21,000 --> 00:04:25,990
And so these are considered instances of that class, or objects.

59
00:04:26,000 --> 00:04:30,990
And these objects are completely functional, and they contain data, and they

60
00:04:31,000 --> 00:04:35,990
contain code in the form of these methods, and that is how you define and

61
00:04:36,000 --> 00:04:46,000
create objects in Python.

