Throughout all of yesterday, we've been using other people's classes.
And we said that a class is simply just a blueprint for creating an eventual
object. In this lesson,
we're going to talk about how we can create our own classes,
so our own blueprints, which we can use to create our own objects.
Now let's go ahead and create our own custom classes in code.
Go ahead and create a new project either in PyCharm or Repl.it wherever you
prefer. And we're going to name it day-17-start,
and then hit create. And again, as always,
once you've created your project, it's time to create your first file
which we're going to name main.py.
And then we can collapse our project sidebar and focus on the code.
So how do we create a class? The syntax looks very simple.
You have the "class" keyword followed by the name of your class and then a colon
.
and then all of the code that's in your class will follow this and it will be
indented. Let's create our very first class in Python.
As I mentioned,
the way that we would create a class is first by using the class keyword.
And then we get to give our class a name.
Let's say that we're building a website and we need a class to model our website's
users.
So this class is basically going to be the blueprint to represent what our
users have and what they can do on our website.
Let's call our class User
and then we finished this declaration with a colon as usual. And everything
else that's going to go inside
this class is going to need to be indented after the colon.
Let's start out with a completely empty class.
Our user class is going to do absolutely nothing for now. However,
because I've created my blueprint
I can already use it to create my first user object.
So let's say I wanted to create a user_1
and I'm going to create it using that class,
which notice it's now being recognized by PyCharm,
and it's got the C symbol next to it. And of course,
to initialize an object from a class,
we have to add the parentheses at the end.
Now we get an error here because there's an indent expected.
So basically Python doesn't like it
when you create something like a class or when you create something like a
function and you have a colon,
but you don't have anything inside that function or class. For example,
if I just immediately wanted to print "hello" afterwards,
I get exactly the same error; indent expected.
It's expecting this function that you've created or this class
you've just created to have some sort of content before you go ahead and do something
else. So how can we fix this? Well,
if we actually really want to leave this function or this class empty,
we can use a keyword which is pass.
And all it does is it just passes. It says, I don't want to have a go right now.
Just continue to the next line of code. And this gets rid of our errors
both in the function declaration
as well as in our class declaration. We've now essentially completed the
first step towards creating our custom classes,
which is writing the declaration and then building an object called user_1 out
of that class. And we've also seen how to name classes in Python.
Now the name of the class should have the first letter of every word
capitalized.
And this particular style of naming in Programming is known as Pascal case.
So if you think of Blaise Pascal as a person,
then we know that everybody's name,
every person's name, has the first letter capitalized and also the first letter
of their surname or their middle name
and basically every subsequent name capitalized. Now,
this is different from another type of casing which you may have come across
which is called camel casing.
And camel casing is only different from Pascal case because the first word is
lowercase, but every subsequent word it has it's first letter capitalized in
exactly the same way as Pascal case. And finally,
we've also come across snake case where all the words are lowercase
but they're separated by
an underscore. In Python programming, you won't see a lot of camel casing.
You will see Pascal case being used for the class names,
snake case being used pretty much to name everything else.