WEBVTT

0
00:00.290 --> 00:07.460
Throughout all of yesterday, we've been using other people's Classes, and we said that a class is

1
00:07.460 --> 00:12.320
simply just a Blueprint for creating an eventual object.

2
00:12.350 --> 00:17.660
In this lesson, we're going to talk about how we can create our own classes.

3
00:17.660 --> 00:23.030
So our own blueprints, which we can use to create our own objects.

4
00:23.630 --> 00:28.280
Now let's go ahead and create our own custom classes in code.

5
00:28.340 --> 00:33.980
Go ahead and create a new project and we're going to name it day-17-start.

6
00:34.250 --> 00:35.960
And then hit create.

7
00:36.440 --> 00:43.280
And again, as always, once you've created your project, it's time to create your first file, which

8
00:43.280 --> 00:44.900
we're going to name main.py.

9
00:44.900 --> 00:49.790
And then we can collapse our project sidebar and focus on the code.

10
00:50.720 --> 00:53.030
So how do we create a class?

11
00:53.240 --> 00:55.770
The syntax looks very simple.

12
00:55.770 --> 01:01.590
You have the 'class' keyword followed by the name of your class, and then a semicolon.

13
01:01.590 --> 01:07.110
And then all of the code that's in your class will follow this and it will be indented.

14
01:07.230 --> 01:10.260
Let's create our very first class in Python.

15
01:10.350 --> 01:17.430
As I mentioned, the way that we would create a class is first by using the 'class' keyword, and then

16
01:17.430 --> 01:19.650
we get to give our class a name.

17
01:19.650 --> 01:27.000
Let's say that we're building a website, and we need a class to model our website's users.

18
01:27.000 --> 01:34.770
So this class is basically going to be the blueprint to represent what our users have and what they

19
01:34.770 --> 01:36.480
can do on our website.

20
01:36.480 --> 01:38.880
So let's call our class, User,

21
01:38.880 --> 01:43.950
and then we finish this declaration with a semicolon as usual.

22
01:44.130 --> 01:51.160
And everything else that's going to go inside this class is going to need to be indented after the semicolon.

23
01:51.250 --> 01:54.640
Let's start out with a completely empty class.

24
01:54.670 --> 01:58.810
Our User class is going to do absolutely nothing for now.

25
01:58.810 --> 02:05.290
However, because I've created my blueprint, I can already use it to create my first User object.

26
02:05.350 --> 02:12.370
So let's say I wanted to create a user_1, and I'm going to create it using that class which notice

27
02:12.400 --> 02:14.560
it's now being recognized by PyCharm,

28
02:14.560 --> 02:16.990
and it's got the C symbol next to it.

29
02:16.990 --> 02:23.140
And of course to initialize an object from a class we have to add the parentheses at the end.

30
02:23.290 --> 02:27.850
Now we get an error here because there's an indent expected.

31
02:27.850 --> 02:34.330
So basically Python doesn't like it when you create something like a class or when you create something

32
02:34.330 --> 02:42.050
like a function and you have a semicolon, but you don't have anything inside that function or class.

33
02:42.050 --> 02:48.230
For example, if I just immediately wanted to print("hello"), afterwards, I get exactly the same error,

34
02:48.230 --> 02:49.520
Indent Expected.

35
02:49.520 --> 02:55.340
It's expecting this a function that you've created, or this class you've just created to have some

36
02:55.340 --> 02:59.060
sort of content before you go ahead and do something else.

37
02:59.180 --> 03:01.010
So how can we fix this?

38
03:01.010 --> 03:06.050
Well, if we actually really want to leave this function or this class empty, we can use a keyword

39
03:06.050 --> 03:07.310
which is 'pass'.

40
03:07.310 --> 03:10.100
And all it does is it just passes.

41
03:10.100 --> 03:12.170
It says, I don't want to have a go right now.

42
03:12.170 --> 03:14.840
Just continue to the next line of code.

43
03:14.840 --> 03:21.410
And this gets rid of our errors both in the function declaration, as well as in our class declaration.

44
03:21.590 --> 03:26.850
We've now essentially completed the first step towards creating our custom classes, which is writing

45
03:26.850 --> 03:32.610
the declaration, and then building an object called user_1 out of that class.

46
03:32.610 --> 03:36.330
And we've also seen how to name classes in Python.

47
03:36.330 --> 03:41.880
Now, the name of the class should have the first letter of every word capitalized,

48
03:41.880 --> 03:48.390
and this particular style of naming in programming is known as PascalCase.

49
03:48.390 --> 03:54.960
So if you think of Blaise Pascal as a person, then we know that everybody's name, every person's name,

50
03:54.960 --> 04:01.800
has the first letter capitalized and also the first letter of their surname or their middle name, and

51
04:01.800 --> 04:04.260
basically every subsequent name capitalized.

52
04:04.290 --> 04:09.600
Now, this is different from another type of casing which you may have come across, which is called

53
04:09.600 --> 04:10.620
camelCasing.

54
04:10.620 --> 04:17.680
And camel casing is only different from pascal case because the first word is lowercase, but every

55
04:17.680 --> 04:23.590
subsequent word has its first letter capitalized in exactly the same way as pascal case.

56
04:23.620 --> 04:29.260
And finally, we've also come across snake_case, where all the words are lowercase, but they're separated

57
04:29.260 --> 04:30.550
by an underscore.

58
04:31.180 --> 04:34.510
In Python programming, you won't see a lot of camel casing.

59
04:34.510 --> 04:40.810
You will see pascal case being used for the class names, snake_case being used pretty much to name

60
04:40.810 --> 04:42.070
everything else.