Reading and changing attributes with dot notation

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

Reading and changing attributes with dot notation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line prints the name attribute of an object called pet?
A print(pet.name)
B print(pet->name)
2. Which line changes the age attribute of pet to 5?
A pet.age = 5
B pet(age) = 5
3. Two objects are made from the same class: dog = Pet('Rex', 3) and cat = Pet('Mia', 2). Then dog.age = 4 runs. What is cat.age?
A 4
B 2
4. Pet only has the attributes name and age. What happens when you run print(pet.colour)?
A It prints None because colour is empty
B AttributeError, because pet has no colour attribute
5. Which is the correct way to read the score attribute of an object called player?
A player.score
B player[score]

Match the Pairs

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

Left columnRight column
1print(pet.name) AThe attribute name is misspelled, so this creates a new wrong attribute and the real one stays unchanged, or reading it later gives an AttributeError.
2pet.age = 5 Bprint(pet.colour)
3Each object keeps its own attribute values, so changing one object does not change another CReads the name attribute stored in the pet object and displays it
4Reading an attribute that the object does not have causes an AttributeError Ddog.age = 7 print(cat.age)
5print(pet[name]) ESquare brackets are used instead of dot notation. An attribute is read with pet.name, not pet[name].
6pet.nmae = 5 FChanges the age attribute of the pet object to 5

Definitions

object.attribute

Fill in the Blanks

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

1.
pet = Pet("Milo", 4) print(___.name)
2.
pet = Pet("Milo", 4) pet_age = 5
3.
dog = Pet("Rex", 3) cat = Pet("Kit", 2) print(___.name) # prints Kit
4.
pet = Pet("Milo", 4) print(pet.colour) # ______________
5.
pet = Pet("Milo", 4) pet.age = pet.age _ 1 print(pet.age) # 5

Spot the Error

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

Question 1
A class Pet:
B def __init__(self, n, a):
C self.name = n
D pet.age = a
E pet = Pet("Rex", 3)
F print(pet.age)
Question 2
A class Pet:
B def __init__(self, n):
C self.name = n
D age = 0
E pet = Pet("Rex")
F print(pet.age)
Question 3
A class Pet: pass
B rex = Pet
C rex.name = "Rex"
D print(rex.name)
Question 4
A class Dog:
B def __init__(self, name):
C self.name == name
D rex = Dog("Rex")
E print(rex.name)
Question 5
A class Cat:
B def __init__(name):
C self.name = name
D tom = Cat("Tom")
E print(tom.name)

Answer Key

Reading and changing attributes with dot notation
Section A: Quick Questions
Q1
A — print(pet.name)
Q2
A — pet.age = 5
Q3
B — 2
Q4
B — AttributeError, because pet has no colour attribute
Q5
A — player.score
Section B: Match the Pairs
Q1
print(pet.name) → Reads the name attribute stored in the pet object and displays it
Q2
pet.age = 5 → Changes the age attribute of the pet object to 5
Q3
Each object keeps its own attribute values, so changing one object does not change another → dog.age = 7 print(cat.age)
Q4
Reading an attribute that the object does not have causes an AttributeError → print(pet.colour)
Q5
print(pet[name]) → Square brackets are used instead of dot notation. An attribute is read with pet.name, not pet[name].
Q6
pet.nmae = 5 → The attribute name is misspelled, so this creates a new wrong attribute and the real one stays unchanged, or reading it later gives an AttributeError.
Section C: Definitions
Q1
object.attribute — Dot notation reads or changes an attribute stored in an object, like print(pet.name) or pet.age = 5. Each object keeps its own values. A missing attribute causes an AttributeError.
Section D: Fill in the Blanks
Q1
pet = Pet("Milo", 4) print(pet.name)
Q2
pet = Pet("Milo", 4) pet.age = 5
Q3
dog = Pet("Rex", 3) cat = Pet("Kit", 2) print(cat.name) # prints Kit
Q4
pet = Pet("Milo", 4) print(pet.colour) # AttributeError
Q5
pet = Pet("Milo", 4) pet.age = pet.age + 1 print(pet.age) # 5
Section E: Spot the Error
Q1
D — Inside the class, the new object is called self, not pet. The name pet does not exist yet, so this causes a NameError. It should be self.age = a.
Q2
D — Without self. this only makes a local variable that disappears when __init__ ends. The object never gets an age attribute, so print(pet.age) causes an AttributeError. It should be self.age = 0.
Q3
B — Without brackets, rex is just another name for the class, not a new object. Each object needs its own attributes, so create it with Pet().
Q4
C — A double == compares values, it does not store one. self.name does not exist yet, so this causes an AttributeError. It should be self.name = name.
Q5
B — The first parameter of __init__ must be self. Without it the object cannot be passed in, so self.name = name cannot store the attribute. It should be def __init__(self, name):.