← ACARA Digital Technologies Years 9–10

Common class mistakes: self and attribute errors

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.

Understanding Common class mistakes: self and attribute errors in ACARA Digital Technologies Years 9–10

What it does: Brackets call the class, which runs __init__ and builds the object. Without brackets you only get the class itself. Inside a method, the attribute lives on the object, so you reach it with self.name rather than just name. The attribute was never stored on the object, so Python cannot find it. This gives an AttributeError. dog.bark() secretly passes dog as an argument. With no self parameter to receive it, the argument count is wrong.

Common mistakes: 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. 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):.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 629–680) and word search (Modules 629–707).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which __init__ line saves the name on the object so other methods can use it?
self.name = namename = name
Matches
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.
Applying
Meanings
Term
self.name = name
Meaning
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.
Practice
class Dog:
Testing
Gaps
def speak():
Check Answer
Spots
2 def __init__(self, name):
3 name = name

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Constellation Wolf' badge — Space Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which __init__ line saves the name on the object so other methods can use it?
A. self.name = name ✓
B. name = name
Without self. the value is only a local variable that disappears when __init__ ends. Use self.name = name.
2. Which method definition lets dog.bark() work without a TypeError?
A. def bark():
B. def bark(self): ✓
Python passes the object in automatically as the first argument, so every method needs self as its first parameter.
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' ✓
The attribute was never stored on the object, so Python cannot find it. This gives an AttributeError.
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
dog.bark() secretly passes dog as an argument. With no self parameter to receive it, the argument count is wrong.
5. Which line creates a Dog object?
A. pet = Dog('Rex') ✓
B. pet = Dog
Without brackets, pet just refers to the class itself. The brackets call the class and build a new object.

Match the Pairs

self.name = name → Saves the value on the object so it can be used later as an attribute
def speak(self): → A method header where self is the first parameter, so Python can pass in the object
Create an object by calling the class with brackets → pet = Dog("Rex")
Read an attribute inside a method using self → return self.name
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.
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.

Key Term

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.

Fill in the Blanks

1. class Dog: def speak(self): print(self.name, "woof")
Hint: Type the parameter that must come first in every method so the object can refer to itself. Leaving it out causes a TypeError about the argument count.
2. class Cat: def rename(self, new): self.name = new
Hint: Type the word that attaches the attribute to the object. Without it the value is lost and you can get an AttributeError later.
3. class Dog: def show(self): print(self.name)
Hint: Type the word that refers to the current object, so the method reads its own name attribute instead of an undefined variable.
4. class Pet: def rename(self, name): self.name = name
Hint: Type the attribute name that stores the value passed in, using the same word as the parameter, so it can be read later with dot notation.
5. class Pet: def info(self): return self.name
Hint: Type the first parameter this method needs. Without it, calling pet.info() gives a TypeError because Python passes the object automatically.

Spot the Error

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

Student with a class code? Join your class →