What a class is
Python comes with types such as int, str and list. A class lets you define a type of your own. Think of it as a blueprint: it describes what a thing is, and later you build actual things from it. A Dog, a BankAccount or a Game are all natural candidates, because each one groups some data with the actions that go with it.
Without classes, a program about dogs ends up with one variable for each dog's name, another for each age, and loose functions that take those variables as arguments. A class bundles all of that under a single name, so the program can talk about "a dog" instead of three unrelated pieces.
This lesson covers only the first step: writing the class itself. Data and behavior are added in the next lessons.
The syntax
An empty class looks like this:
class ClassName:
pass
Three details matter:
- The keyword
classtells Python that a new type is being defined. - The name comes next. Python programmers write class names in PascalCase: every word starts with a capital letter and there are no underscores, as in
Dog,BankAccountorHighScore. Functions and variables use lowercase with underscores instead, so the capital letter tells a reader at a glance that a name is a class. - A colon ends the header line, and the body is indented below it. Python uses indentation, not braces, to decide what belongs to the class.
Why the body needs pass
A class body cannot be empty. If you write the header and nothing below it, Python reports a syntax error because it expects an indented block. The statement pass does nothing at all, but it is a valid statement, so it fills the block. It is a placeholder that says "nothing here yet".
A docstring, a string literal on its own as the first line of the body, would also make the block valid. In this lesson pass is the simplest choice.
The lesson's exercise
The task is to define an empty class named Dog. The code below the class is already written: it creates one instance of the class and prints a confirmation. If the class does not exist, the line that creates the instance fails with a NameError, so the program never reaches the message.
- Write the header. Type
class Dog:with a capitalDand the colon at the end. The name must match exactly, because the code below refers toDog. - Indent the body. On the next line, indent by four spaces and write
pass. - Leave the rest alone. Calling
Dog()is covered in the next lesson. For now, you only need to know that it builds an object from your class.
The finished program:
class Dog:
pass
dog = Dog()
print("Defined the Dog class!")
It prints:
Defined the Dog class!
What an empty class can already do
Even with only pass in it, the class is a real type. You can create objects from it, check their type, and compare them:
class Dog:
pass
a = Dog()
b = Dog()
print(isinstance(a, Dog))
print(a is b)
True
False
Each call to Dog() produces a separate object, so a is b is False. An empty class has no data and no behavior of its own, which is why the following lessons add attributes and methods to it.
Common mistakes
- Forgetting the colon.
class Dogwithout the:is a syntax error. - Forgetting the indented body. A header with nothing under it is also a syntax error. Add
pass. - Using the wrong capitalization.
dogandDogare different names in Python. A class nameddogworks, but it breaks the convention and the lesson's code expectsDog. - Unindented
pass. Ifpasssits at the left margin, it is outside the class and the class has no body.
Key takeaways
- A class is a blueprint for a new type, defined with the
classkeyword. - The header is the name, in PascalCase, followed by a colon. The body is indented.
- The body cannot be empty, and
passis the placeholder that makes it valid. - Calling the class, as in
Dog(), creates an object from it.