Class Exercises

Class basics

First class

Defining class is a recipe. Take a look at the syntax:

1
2
3
class Dog:
    name = 'default name'
    age = 0

The important part to notice is the class Dog:. This is what indicates the beginning of the code block. After the class is defined, two variables are declared. These variables are inside the class. Think of them like files in a folder.

1
2
3
4
5
class Dog:
    name = 'default name'
    age = 0

fido = Dog()

This code instantiates the class. This means you are using the recipe to create a new object.

To repeat the vocabulary:

  • instantiate: use the recipe to create an object
  • object: a specific instance of a class. think of this like a cookie from a cookie recipe.

Using the first class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Dog:
    name = 'default name'
    age = 0

fido = Dog()

print("1. Fido's name: ", fido.name)
fido.name = "Fido"

print("2. Fido's name: ", fido.name)

george = Dog()
print("3. George's name: ", george.name)
print("3. Fido's name: ", fido.name)

george.name = "George"
print("4. George's name: ", george.name)
print("4. Fido's name: ", fido.name)

Run this example.

You will see that changing the internal properties of fido and george stay inside fido and george! This is another example of scope. Just like inside functions are local scope, inside objects are local scope!

Getting access to internal variables

1
2
3
4
5
6
7
8
9
class Dog:
    name = 'default name'
    age = 0

def speak(some_dog):
    print("My name is {}. Bow wow!".format(some_dog.name))

fido = Dog()
speak(fido)

We can write functions which use the fido object as its argument and access the internal variables!

Getting access to internal variables

You can see from the last example that you access the internal variables using the dot notation. But, what if you wanted to write a function inside the object? How can you access the variables?

Let’s try this:

1
2
3
4
5
6
7
8
9
class Dog:
    name = 'default name'
    age = 0

    def speak():
        print("My name is {}. Bow wow!".format(name))

fido = Dog()
fido.speak()

Do you think this will work? Nope! Scope doesn’t let us do that!

There is a second reason why the code above won’t work and that reason is also what solves things!

1
2
3
4
5
6
7
8
9
class Dog:
    name = 'default name'
    age = 0

    def speak(self):
        print("My name is {}. Bow wow!".format(self.name))

fido = Dog()
fido.speak()

When you use the function that is inside an object, python adds a variable without you having to do anything! That variable is called the self variable. This is just like having the function outside of the class, except that Python puts the self variable there automatically, so we don’t have to.

Exercises

Exercise 1

  • Choose a new animal and write a class for it.
    • Pay attention to the pattern above.
    • The important part is the class keyword and the name.
  • The class should have 2 internal variables.

You should write code that also uses your new class. For instance:

1
2
3
4
5
6
7
8
class Dog:
    name = 'default name'
    age = 0

pet1 = Dog()

pet1.name = "Fido"
print("First pet's name: ", pet1.name)

Exercise 2

You should have written code that uses your class for the last exercise. Write a new function that takes will run this code. It will take as input one variable, expecting that variable to be an object from your class. It should print out all important variables in your class!

For example:

1
2
def info(some_animal):
    print("Animal's name: ", animal.name)

Exercise 3

Add an extra variable called sleep to your class. It should start off as False. Then, write two functions, called goto_sleep and wake_up. The functions will take as input a single variable (like in Exercise 2). They will check if sleep is True or False:

  • in goto_sleep, if sleep is True
    • nothing needs to happen
    • it should print that the animal is already asleep
  • in goto_sleep, if sleep is False
    • the variable sleep should be set to True
    • it should print that the animal is now sleeping

The exact same pattern happens in wake_up, except the other way around:

  • in wake_up, if sleep is False
    • nothing needs to happen
    • it should print that the animal is already awake
  • in wake_up, if sleep is True
    • the variable sleep should be set to False
    • it should print that the animal is now waking up

Exercise 4

Put the functions from Exercise 2 and Exercise 3 inside the class. The code remains exactly the same, except that the variable changes from some_animal will change to self and Python will start to pass it in for us. See the example above again for speak to see what I mean.

If it were the Dog class, your class should structurally look like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Dog:

    def info(self):
        print("correct code is here!")

    def goto_sleep(self):
        print("correct code is here!")

    def wake_up(self):
        print("correct code is here!")