Common class mistakes: self and attribute errors

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Common class mistakes: self and attribute errors

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which __init__ line saves the name on the object so other methods can use it?
A self.name = name
B name = name
2. Which method definition lets dog.bark() work without a TypeError?
A def bark():
B def bark(self):
3. __init__ only has the line name = name (no self.). What happens at print(p.name)?
A It prints the name that was passed in
B AttributeError: the object has no attribute 'name'
4. A method is written def bark(): and then called as dog.bark(). What happens?
A TypeError: takes 0 positional arguments but 1 was given
B It runs normally and prints nothing
5. Which line creates a Dog object?
A pet = Dog('Rex')
B pet = Dog

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight 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.

Definitions

self.name = name

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
class Dog: def speak(____): print(self.name, "woof")
2.
class Cat: def rename(self, new): ____.name = new
3.
class Dog: def show(self): print(____.name)
4.
class Pet: def rename(self, name): self.____ = name
5.
class Pet: def info(____): return self.name

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Dog:
B def __init__(self, name):
C name = name
D def bark(self):
E print(self.name)
Question 2
A class Cat:
B lives = 9
C def speak():
D print('Meow')
Question 3
A pet = Dog
B print(pet.name)
C pet.bark()
Question 4
A p = Player('Zed')
B p.score = p.score + 5
C print(p.name, p.score)
Question 5
A class Wallet:
B def __init__(self):
C self.cash = 0
D def add(amount, self):
E self.cash += amount

Answer Key

Common class mistakes: self and attribute errors
Section A: Quick Questions
Q1
A — self.name = name
Q2
B — def bark(self):
Q3
B — AttributeError: the object has no attribute 'name'
Q4
A — TypeError: takes 0 positional arguments but 1 was given
Q5
A — pet = Dog('Rex')
Section B: Match the Pairs
Q1
self.name = name → Saves the value on the object so it can be used later as an attribute
Q2
def speak(self): → A method header where self is the first parameter, so Python can pass in the object
Q3
Create an object by calling the class with brackets → pet = Dog("Rex")
Q4
Read an attribute inside a method using self → return self.name
Q5
def __init__(name): self.name = name → The self parameter is missing, so Dog("Rex") gives a TypeError for the wrong number of arguments and self is not defined.
Q6
def __init__(self, name): name = name → Without self. the value is only saved in a local variable, so the object has no name and reading pet.name causes an AttributeError.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
class Dog: def speak(self): print(self.name, "woof")
Q2
class Cat: def rename(self, new): self.name = new
Q3
class Dog: def show(self): print(self.name)
Q4
class Pet: def rename(self, name): self.name = name
Q5
class Pet: def info(self): return self.name
Section E: Spot the Error
Q1
C — Without self. the value is stored in a local variable that disappears when __init__ ends, so the object has no name attribute and dog.name raises an AttributeError. It should be self.name = name.
Q2
C — Every method needs self as its first parameter. Python passes the object in automatically, so cat.speak() raises a TypeError (0 positional arguments but 1 was given). It should be def speak(self):.
Q3
A — Without brackets, pet just refers to the class itself and no object is created. pet.name then raises an AttributeError. The class must be called with brackets: Dog('Rex').
Q4
A — __init__ needs a name and a score, but only one argument was given. This causes a TypeError for the wrong argument count (missing 1 required positional argument). It should be Player('Zed', 10).
Q5
D — self must be the FIRST parameter. Python passes the object into the first slot, so amount would receive the object and self would receive the number. self.cash then fails with an AttributeError. It should be def add(self, amount):.