Creating a Class
00:00
Creating a class in Python is not particularly complicated. Here you can see two classes being defined. Start off with the class
keyword and then put in the name of the class.
00:12
In this case, the class is Point
.
00:15
After the name of the class, you can put a colon (:
), and then on a new line that’s indented, you can write your class. And in this example, you’re just passing the pass
keyword, which is like a placeholder.
00:29
It just means nothing. It just, if you were to leave this blank, that would create an error. But if you leave pass
as a placeholder, then you can create this blank class. Now here’s another example.
00:40
You’re just changing the name of the class from Point
to Doggo
.
00:46
So let’s create a class together here on the IDLE Shell. Remember you start off with the class
keyword and then think of a name. Let’s do Cat:
, press Enter.
00:59 That starts an indented line automatically for us.
01:02
We’ll just put in the pass
keyword as a placeholder now. Press Enter a couple more times, and we’ve got a class. If you just put in Cat
on the REPL, you’ll see that now a identifier for a class comes up on the output.
01:23 Now you’ll want to bear in mind that classes in Python have a naming convention. You don’t have to follow this. You can name classes in whatever way you want, but there are a lot of people using classes, and there is a convention that is called CapitalizedWords, or also CamelCase, or also PascalCase.
01:40 Every word is capitalized, including the first letter. There is no underscore between words. You just start with a new capital letter. CamelCase is sometimes understood as the first starting word not having a capital letter, but CamelCase is often used in both situations.
01:57 In any case, the key thing to take away from this is that when you’re creating a class, the convention is to start off with a capital letter, and then every word after that starts with a capital letter, no underscore or space between the words.
Become a Member to join the conversation.