Public attributes and methods

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

Public attributes and methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which attribute is public?
A self._name
B self.name
2. Which line reads a public attribute from outside the class (pet is an object)?
A print(pet.name)
B print(self.name)
3. True or false: a public method can be called from outside the class, e.g. pet.speak().
A True
B False
4. Which method name is public?
A def speak(self):
B def _speak(self):
5. Which line changes a public attribute from outside the class?
A self.name = 'Rex'
B pet.name = 'Rex'

Match the Pairs

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

Left columnRight column
1self.name = name AThe attribute is accessed without an object. Outside the class a public attribute needs the object and a dot, like pet.name.
2def speak(self): BDefines a public method that can be called from inside or outside the class
3Code outside the class can read a public attribute using the object name and a dot CCreates a public attribute with no leading underscore, so any code can read or change it
4Code outside the class can call a public method on an object DThe leading underscore makes the attribute protected by convention, not public. Use self.name to make it public.
5self._name = name # meant to be public Epet.speak()
6print(name) # outside the class Fprint(pet.name)

Definitions

self.name

Fill in the Blanks

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

1.
pet = Pet("Milo") pet._____() print(pet.name)
2.
pet = Pet("Milo") print(___.name)
3.
class Pet: def speak(____): print("Woof!")
4.
def intro(self): ____.speak()
5.
pet = ___("Milo") print(pet.name) pet.speak()

Spot the Error

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

Question 1
A class Pet:
B def __init__(self, name):
C name = name
D pet = Pet("Rex")
E print(pet.name)
Question 2
A class Pet:
B def __init__(self, name):
C self.name = name
D def speak():
E print(self.name, "woofs")
F Pet("Rex").speak()
Question 3
A class Dog:
B def _bark(self):
C print("Woof!")
D rex = Dog()
E rex.bark()
Question 4
A class Car:
B def __init__(self, make)
C self.make = make
D car = Car("Kia")
E print(car.make)
Question 5
A class Player:
B def __init__(self):
C self.__score = 0
D p = Player()
E print(p.score)

Answer Key

Public attributes and methods
Section A: Quick Questions
Q1
B — self.name
Q2
A — print(pet.name)
Q3
A — True
Q4
A — def speak(self):
Q5
B — pet.name = 'Rex'
Section B: Match the Pairs
Q1
self.name = name → Creates a public attribute with no leading underscore, so any code can read or change it
Q2
def speak(self): → Defines a public method that can be called from inside or outside the class
Q3
Code outside the class can read a public attribute using the object name and a dot → print(pet.name)
Q4
Code outside the class can call a public method on an object → pet.speak()
Q5
self._name = name # meant to be public → The leading underscore makes the attribute protected by convention, not public. Use self.name to make it public.
Q6
print(name) # outside the class → The attribute is accessed without an object. Outside the class a public attribute needs the object and a dot, like pet.name.
Section C: Definitions
Q1
self.name — A public member has no leading underscore, so code inside or outside the class can access it, e.g. self.name in the class or pet.name outside it.
Section D: Fill in the Blanks
Q1
pet = Pet("Milo") pet.speak() print(pet.name)
Q2
pet = Pet("Milo") print(pet.name)
Q3
class Pet: def speak(self): print("Woof!")
Q4
def intro(self): self.speak()
Q5
pet = Pet("Milo") print(pet.name) pet.speak()
Section E: Spot the Error
Q1
C — Without self., this only sets a local variable, so no public attribute is created. It should be self.name = name so pet.name works outside the class.
Q2
D — Every method needs self as its first parameter so it can use self.name. It should be def speak(self):.
Q3
B — The leading underscore means the method is not public, so rex.bark() fails from outside the class. A public method has no underscore: def bark(self):.
Q4
B — The def line is missing its colon. It should be def __init__(self, make): so the public attribute self.make can be set.
Q5
C — The double underscore makes score private, so p.score fails outside the class. A public attribute has no leading underscore: self.score = 0.