Write Classes Using Python Syntax
Now you know what a class is, let’s talk about how to write one in Python.
We use the class
keyword, and a combination of function declarations (def
), and variable assignment (x = 3
) statements.
Here’s an example:
class Rectangle:
width = 3
height = 2
def calculate_area(self):
return self.width * self.height
Each class declaration starts with the class
keyword, followed by that class’s name. In the example above, the class is named Rectangle.
All of the variables and methods that make up that class (attributes) are defined underneath, indented to show that it’s part of the class’s “scope.”
When you declare a method, you use the “def” keyword - just like you do with functions. You can see an example of that in the calculate_area
method above. The first parameter of a method is always self
, and you can use it to access that object’s attributes. For example, you use it in calculate_area
to multiply a rectangle object’s width by its height.
Finally, you have variables - these provide default values that’ll exist within every object. You set the default value for width and height, which means that every rectangle will have those values on creation.
That’s it! All the classes you’ll write will be based on these building blocks (and a couple of others we’ll introduce later.) You could add more methods to your Rectangle class (such as the constructor we’ll talk about in the next section) if you needed to.
Call a Constructor
There is a special kind of method called a constructor. Every class has one, and it's used to create objects from that class. In Python, the constructor is a method called __init__
, and it can be used a bit like this:
class Rectangle:
def __init__(self, length, width, color=”red”):
self.length = length
self.width = width
self.color = color
Constructors allow you to pass in initial values to an object that’s being created. Remember, in the previous example, when we set default values for every rectangle? By setting those values in the constructor instead, you can initialize objects with different lengths and widths.
To create a new rectangle object in the above Rectangle class, you need to provide it a length, width, and color. We’ll talk more about creating objects in the next chapter.
What’s this color=red
in the method definition?
It’s is a convenient shortcut - you can call this constructor with either two or three parameters, and if you don’t provide a color, the default value (red) is used instead. You can use these optional parameters with any method or function you write, but make sure that your optional parameters come after the other (concrete) parameters in the list.
Most classes will need a constructor. If you don’t provide one, Python will add a default constructor that does nothing.
This default constructor is part of the object class that all classes inherit, but we’ll talk about it later.
Your Turn: Write Classes
Context :
Now it’s time to translate those plans from the last chapter into code.
Instructions :
In your own development environment, write three classes, one for each of the classes you identified and planned in the last chapter’s exercise.
You won’t know how to implement some of the methods yet, and that’s OK; just leave the method body empty. In Python, you can do this with the keyword “pass,” like this:
Def method_stub(self, length):
pass
It’s a good idea to put all of the classes you’re writing into the same Python file. We’ll talk in more detail about structuring and dividing your code up throughout the course.
Let’s Recap!
Make a class using the
class
keyword.The
__init__
method is known as the constructor.Constructors are called when an object is created.
Now that you know how to write a class, it’s time to move on to creating and using objects!