Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1self.name = name | AWithout self. the value is only saved in a local variable, so the object has no name and reading pet.name causes an AttributeError. | |
| 2def speak(self): | Breturn self.name | |
| 3Create an object by calling the class with brackets | CSaves the value on the object so it can be used later as an attribute | |
| 4Read an attribute inside a method using self | DA method header where self is the first parameter, so Python can pass in the object | |
| 5def __init__(name): self.name = name | Epet = Dog("Rex") | |
| 6def __init__(self, name): name = name | FThe self parameter is missing, so Dog("Rex") gives a TypeError for the wrong number of arguments and self is not defined. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
self.name = name — Saves a value on the object. Without self. the attribute is lost (AttributeError), and a method missing self as first parameter gives a TypeError for wrong argument count.class Dog:
def speak(self):
print(self.name, "woof")class Cat:
def rename(self, new):
self.name = newclass Dog:
def show(self):
print(self.name)class Pet:
def rename(self, name):
self.name = nameclass Pet:
def info(self):
return self.name