Introduction to classes

Question Click to View Answer

What does the following code print?

class Airplane:
    purpose = "fly"

a = Airplane()

print(a.purpose)

"fly" is printed.

The purpose variable is assigned to "fly" in the Airplane class namespace.

Variables defined in the class namespace can be accessed with "dot notation" - a.purpose is pronounced as "a dot purpose".

The class keyword is used to define a class called Airplane. Classes are "instantiated" to create objects.

a = Airplane() creates an instance of the Airplane class and assigns the object to the variable a.

You'll need to study object oriented terminology and basic examples a lot if these concepts are new. Repetition is key!

What does the following code print?

class Mouse:
    sound = "squeak"

mousie = Mouse()

mousie.name = "fred"

print(mousie.name)

"fred" is printed.

A Mouse class is defined. The Mouse class is instantiated and the resulting object is assigned to the variable mousie.

mousie.name = "fred" uses dot notation to add a name attribute to the mousie object. We're mutating the mousie object by adding an attribute with data.

What does the following code print?

class Chair:
    def speak(self):
        return "sit on me"

cool_chair = Chair()

print(cool_chair.speak())

"sit on me" is printed.

The Chair class is defined an a speak() method is defined in the class namespace.

speak() is an instance method meaning that it will be run on instances of the Chair class. speak() takes an argument called self which is a reference to the object that the method is being invoked on.

The cool_chair variable is assigned to an instance of the Chair class.

The speak() method is called on the cool_chair object and the resulting string is printed.

Identify the class, method, and class instance in the following code.

class Television:
    def turn_on(self):
        return "i am on"

bunny = Television()

print(bunny.turn_on())

Television is the class.

turn_on() is the method. The first argument of methods is always self.

The variable bunny is assigned to an instance of the Television class.

What does the following code print?

class Headphones:
    def volume(self):
        return 10

my_headphones = Headphones()

print(Headphones.volume(my_headphones))

print(my_headphones.volume())

10 is printed twice.

The Headphones class is defined with a volume() method.

Headphones.volume(my_headphones) calls the volume() method directly on the Headphones class and explicitly passes the my_headphones object as an argument to the method.

print(my_headphones.volume()) calls the volume() method on the my_headphones object and does not pass in an argument. Python automatically passes in the object as the first argument when a method is called.

Headphones.volume(my_headphones) and print(my_headphones.volume()) are two different syntaxes that are run identically internally.

What does the following code print?

class Wire:
    def length():
        return "short"

charger = Wire()

print(charger.length())

This code raises a TypeError with the following message: length() takes 0 positional arguments but 1 was given.

We forgot to supply self as the first argument to the length() method. This code will work:

class Wire:
    def length(self):
        return "short"

What does the following code print?

class BossBaby:
    def head_size(self, word):
        return f"his head is {word}"

b = BossBaby()

print(b.head_size("large"))

"his head is large" is printed.

The BossBaby class has a head_size() method that takes two parameters: self and word.

In this example the head_size() method is called with the "large" argument.