1
00:00:00,000 --> 00:00:05,990
Object-oriented programming is a very powerful model for reusing code.

2
00:00:06,000 --> 00:00:11,990
Let's take a look at how objects and classes are defined and used in Python.

3
00:00:12,000 --> 00:00:13,990
We'll start with this simpleoop.

4
00:00:14,000 --> 00:00:16,990
oop stands for Object-oriented programming.

5
00:00:17,000 --> 00:00:26,990
We'll make a working copy of this, and we'll go ahead and open the working copy.

6
00:00:27,000 --> 00:00:31,990
And let's just take a look at what's in here right now.

7
00:00:32,000 --> 00:00:38,990
This is a class, and a class, in Python, as in many object-oriented paradigms, a

8
00:00:39,000 --> 00:00:42,990
class is the definition that is used to create an object.

9
00:00:43,000 --> 00:00:44,990
It's sometimes called a blueprint.

10
00:00:45,000 --> 00:00:49,990
So objects are instances of classes.

11
00:00:50,000 --> 00:00:54,990
So when you take a class, here's the class that we've defined up here, class

12
00:00:55,000 --> 00:01:00,990
Fibonacci, and you assign it to a variable, that's called instantiating, or

13
00:01:01,000 --> 00:01:02,990
creating an instance, of that class,

14
00:01:03,000 --> 00:01:04,990
and the instance itself, in this case,

15
00:01:05,000 --> 00:01:09,990
it's just called f, that instance is called an object.

16
00:01:10,000 --> 00:01:12,990
An object is an instance of a class.

17
00:01:13,000 --> 00:01:17,990
So the class is the definition, and here we have class, and it's named Fibonacci,

18
00:01:18,000 --> 00:01:20,990
and it has these parentheses, and we'll talk later about what goes in the

19
00:01:21,000 --> 00:01:23,990
parentheses if anything. Right now nothing needs to go into them.

20
00:01:24,000 --> 00:01:29,990
The colon introduces the blocks or suites of code that are indented, and

21
00:01:30,000 --> 00:01:32,990
therefore part of the class definition.

22
00:01:33,000 --> 00:01:37,990
And here, we have two functions defined as part of this class.

23
00:01:38,000 --> 00:01:42,990
And each of these functions, you'll notice that its first argument is self.

24
00:01:43,000 --> 00:01:46,990
And here we have one that's just got self, and here we have one that's got self

25
00:01:47,000 --> 00:01:49,990
and some other variables.

26
00:01:50,000 --> 00:01:52,990
self is the common name for that first argument.

27
00:01:53,000 --> 00:01:53,990
It doesn't have to be self.

28
00:01:54,000 --> 00:01:55,990
It's traditionally self.

29
00:01:56,000 --> 00:02:00,990
And what it is is a reference to the instantiated object.

30
00:02:01,000 --> 00:02:06,990
So when we use the object f, and here we're calling a method or a function

31
00:02:07,000 --> 00:02:11,990
inside of the object f, that's what this dot notation does, f.series().

32
00:02:12,000 --> 00:02:17,990
It calls the series function or method, but it calls the copy of it that's part

33
00:02:18,000 --> 00:02:20,990
of the object, the instantiated object.

34
00:02:21,000 --> 00:02:27,990
And its first argument is actually a reference to the object f. And so, we can

35
00:02:28,000 --> 00:02:33,990
assign and use variables that are encapsulated within that object and that are

36
00:02:34,000 --> 00:02:35,990
actually carried around.

37
00:02:36,000 --> 00:02:40,990
So if we had different instances of the same class, they would have separate

38
00:02:41,000 --> 00:02:48,990
variables, separate values in self.a and self.b. This init function, or method, is

39
00:02:49,000 --> 00:02:51,990
actually special. This is called a Constructor.

40
00:02:52,000 --> 00:02:56,990
And this gets called when f gets assigned.

41
00:02:57,000 --> 00:03:04,990
So f gets assigned Fibonacci, and that creates an object called f, which is an

42
00:03:05,000 --> 00:03:06,990
instance of Fibonacci.

43
00:03:07,000 --> 00:03:13,990
As that object is created, init is called, and init is called with a reference to

44
00:03:14,000 --> 00:03:18,990
self and with these variables, and here we have 0 and 1.

45
00:03:19,000 --> 00:03:21,990
You can define these however you want to because you're writing this

46
00:03:22,000 --> 00:03:24,990
constructor. The constructor is optional.

47
00:03:25,000 --> 00:03:27,990
If you don't need it, you don't need to create it.

48
00:03:28,000 --> 00:03:32,990
But in this case, we used a constructor so that we could demonstrate how this works.

49
00:03:33,000 --> 00:03:36,990
And what this is doing here is it simply creating some default initial values

50
00:03:37,000 --> 00:03:41,990
for a and b, which are going to used as part of the Fibonacci series.

51
00:03:42,000 --> 00:03:48,990
And then we have this second method, or function, and this one is actually a

52
00:03:49,000 --> 00:03:52,990
generator. You see it uses the yield statement.

53
00:03:53,000 --> 00:03:57,990
And so that generates an iterator, which is used here in this for loop, and so

54
00:03:58,000 --> 00:04:01,990
for r in f.series() is going to give us a series of all the Fibonacci numbers

55
00:04:02,000 --> 00:04:04,990
that are less then 100.

56
00:04:05,000 --> 00:04:13,990
So let's go ahead and run it, and there we have our Fibonacci series up to 89,

57
00:04:14,000 --> 00:04:17,990
which is the last Fibonacci number that is less than 100.

58
00:04:18,000 --> 00:04:23,990
So here we have defined a class called Fibonacci, we have instantiated it,

59
00:04:24,000 --> 00:04:30,990
creating an object, called f, and in that object, we called this generator function

60
00:04:31,000 --> 00:04:33,990
and we got a series of Fibonacci numbers.

61
00:04:34,000 --> 00:04:44,000
So that is a simple example of a class and an object in Python.

