4.4. Creating and Using a Class

You can model almost anything using classes. Let's start by writing a simple class, Dog, that represents a dog (it can be any dog).

Creating the Dog Class

Every instance created form the Dog class will store a name and an age, and we'll give each dog the ability to sit() and roll_over() later on.

In the first line, we define a class called Dog. (In Python, class starts with capital letter). In the second line, we write a docstring describing what this class does.

The __init__() method

The __init__() method is a special method that Python runs automatically whenever we create a new instance based on the Dog class. We define that method to have three parameters: self, name, and age. The self parameter is a must, and it has to come first before the other parameters.

When we make an instance of Dog, Python will call the __init__() method. We'll pass Dog() a name and an age as arguments; self is passed automatically, so we don't need to pass it. See the example.

The two variables defined in def each have the prefix self. Any variable prefixed with self is available to every method in the class. The line self.name = name takes the value associated with the parameter name and assigns it to the variable name. (Same process with self.age = age). Variables that are accessible through instances like this are called attributes.

Making an instance from a Class

The class Dog is a set of instructions that tells Python how to make individual instances representing specific dogs. Let's make an instance representing dog.

Accessing Attributes

To access the attributes of an instance, you use dot notation. This syntax demonstrates how Python finds an attribute's value. Here, Python looks at the instance my_dog and then finds the attribute name and age associated with my_dog.

> At , we tell Python to create a dog: 'Willie' and it's age is 6. In this line, it runs the __init__() method in Dog with the arguments `Willie` and 6. Python then returns an instance representing this particular dog, and assign it in my_dog.
> At , we access the value of my_dog's attribute name by writing my_dog.name. At ❸ we use the same approach to work with the attribute age.

Calling method

After we create an instance from the class Dog, we can use dot notation to call any method defined in Dog. Let's make our dog sit and roll over.

We define other two methods: sit() and roll_over(). Because these methods don't need additional information to run, we just define them to have one parameter, self. For now, they simply print a message saying the dog is sitting or rolling over.


> To call a method, give the name of the instance (in this case, my_dog) and the method you want to call, separated by a dot. When Python reads my_dog.sit(), it looks for the method sit() in the class Dog and runs that code. Python interprets the line my_dog.roll_over() in the same way.

Creating Multiple Instances

You can create as many instances from a class as you need. Let's create a second dog called your_dog:


> In this example, we create a dog named Willie and a dog named Lulu. Each dog is a separate instance with its own set of attributes, capable of the same set of actions. You can make as many instances from one class as you need, as long as you give each instance a unique variable name.

Exercise 4.4

  1. Restaurant
    Make a class called Restaurant. The __init__() method for Restaurant should store two attributes: a restaurant_name and a cuisine_type. Make a method called describe_restaurant() that prints these two pieces of information, and a method called open_restaurant() that prints a message indicating that the restaurant is open.
    Make an instance called restaurant from your class. Print the two attributes individually, and then call both methods.


  1. Three Restaurants
    Start with your class from no.1. Create three different instances from the class, and call describe_restaurant() for each instance.


  1. Users
    Make a class called User. Create two attributes called first_name and last_name, and then create several other attributes that are typically stored in a user profile. Make a method called describe_user() that prints a summary of the user's information. Make another method called greet_user() that prints a personalized greeting to the user.

Create several instances representing different users, and call both methods for each user.