Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a protected attribute in Python?
A protected attribute is a variable in a class that is intended to be accessed only within the class and its subclasses. It is marked by a single underscore prefix (e.g., <code>_attribute</code>).
Click to reveal answer
beginner
How do you define a protected attribute in a Python class?
You define it by prefixing the attribute name with a single underscore, like self._name = value. This signals it is for internal use only.
Click to reveal answer
beginner
Can protected attributes be accessed from outside the class in Python?
Yes, they can be accessed from outside, but it is discouraged. The single underscore is a convention to indicate 'please don’t touch this' rather than a strict rule.
Click to reveal answer
intermediate
Why use protected attributes instead of public ones?
Protected attributes help organize code by showing which parts are internal and should not be changed directly. This helps avoid bugs and makes maintenance easier.
Click to reveal answer
beginner
Example: What does this code do?
<pre>class Car:
def __init__(self, model):
self._model = model
car = Car('Toyota')
print(car._model)</pre>
This code creates a Car object with a protected attribute _model. It then prints the model name 'Toyota'. Accessing _model works but is discouraged outside the class.
Click to reveal answer
What prefix is used to mark a protected attribute in Python?
ANo prefix
BDouble underscore (__) prefix
CA single underscore (_) prefix
DA dollar sign ($) prefix
✗ Incorrect
Protected attributes use a single underscore (_) prefix to indicate they are for internal use.
Can you access a protected attribute from outside its class?
AYes, but it is discouraged
BNo, it is impossible
COnly if you use special methods
DOnly in subclasses
✗ Incorrect
Protected attributes can be accessed from outside, but the single underscore signals you should not do it.
What does a protected attribute help with in code?
AMaking the attribute public
BIndicating internal use and protecting from accidental changes
CEncrypting the attribute
DPreventing any access
✗ Incorrect
Protected attributes show which parts of code are internal and should not be changed directly.
Which of these is a protected attribute name?
A_speed
Bspeed
C__speed
D$speed
✗ Incorrect
A single underscore prefix like '_speed' marks a protected attribute.
What happens if you try to access a protected attribute from outside the class?
AThe program crashes
BPython throws an error
CThe attribute is hidden
DYou can access it, but it’s discouraged
✗ Incorrect
Python allows access but the underscore warns you not to do it.
Explain what a protected attribute is and why you might use it in a Python class.
Think about how you tell others not to change something in your code.
You got /4 concepts.
Describe the difference between protected and private attributes in Python.
Focus on the number of underscores and how Python treats them.
You got /4 concepts.
Practice
(1/5)
1. What does a single underscore prefix (e.g., _value) in a Python class attribute indicate?
easy
A. It is a public attribute accessible everywhere.
B. It is a protected attribute meant for internal use within the class and subclasses.
C. It is a private attribute that cannot be accessed outside the class.
D. It is a special Python keyword for constants.
Solution
Step 1: Understand underscore usage in Python
A single underscore prefix means the attribute is intended for internal use, signaling protection but not strict privacy.
Step 2: Differentiate from private and public
Private attributes use double underscores, public have no underscore, and constants are uppercase without underscores.
Final Answer:
It is a protected attribute meant for internal use within the class and subclasses. -> Option B
Quick Check:
Single underscore = protected attribute [OK]
Hint: Single underscore means 'protected' by convention [OK]
Common Mistakes:
Confusing single underscore with private (double underscore)
Thinking single underscore makes attribute inaccessible
Assuming single underscore means public attribute
2. Which of the following is the correct way to define a protected attribute named _count inside a Python class?
easy
A. self._count = 0
B. count = 0
C. self.__count = 0
D. self.count = 0
Solution
Step 1: Identify protected attribute syntax
Protected attributes start with a single underscore, so self._count is correct.
Step 2: Check other options
self.count is public, self.__count is private, and count = 0 is a local variable, not an attribute.
Final Answer:
self._count = 0 -> Option A
Quick Check:
Protected attribute = single underscore prefix [OK]
Hint: Use single underscore for protected attributes inside classes [OK]
Common Mistakes:
Using no underscore for protected attribute
Using double underscore for protected instead of private
Protected attributes can be accessed outside the class, though it is discouraged.
Step 2: Check code behavior
The attribute _value is set to 10 and printed directly, so output is 10.
Final Answer:
10 -> Option C
Quick Check:
Protected attribute accessible outside class = 10 [OK]
Hint: Protected attributes can be read outside class [OK]
Common Mistakes:
Expecting AttributeError when accessing protected attribute
Confusing protected with private attributes
Thinking protected attributes are hidden
4. Find the error in this code that tries to access a protected attribute:
class Parent:
def __init__(self):
self._data = 5
class Child(Parent):
def print_data(self):
print(self.data)
c = Child()
c.print_data()
medium
A. TypeError because of wrong method call
B. SyntaxError due to missing colon
C. No error, prints 5
D. AttributeError because self.data does not exist
Solution
Step 1: Check attribute names in Parent and Child
Parent defines self._data, but Child tries to print self.data, which does not exist.
Step 2: Understand error type
Accessing a non-existent attribute causes AttributeError at runtime.
Final Answer:
AttributeError because self.data does not exist -> Option D
Quick Check:
Wrong attribute name = AttributeError [OK]
Hint: Check exact attribute names when accessing [OK]
Common Mistakes:
Ignoring underscore in attribute name
Assuming protected attribute is private and inaccessible
Confusing syntax errors with attribute errors
5. You want to create a class BankAccount with a protected attribute _balance that can be safely accessed and updated only by subclasses. Which code snippet correctly implements this?
hard
A. class BankAccount:
def __init__(self, balance):
self._balance = balance
class SavingsAccount(BankAccount):
def deposit(self, amount):
self._balance += amount
acc = SavingsAccount(100)
acc.deposit(50)
print(acc._balance)
B. class BankAccount:
def __init__(self, balance):
self.__balance = balance
class SavingsAccount(BankAccount):
def deposit(self, amount):
self.__balance += amount
acc = SavingsAccount(100)
acc.deposit(50)
print(acc.__balance)
C. class BankAccount:
def __init__(self, balance):
self.balance = balance
class SavingsAccount(BankAccount):
def deposit(self, amount):
self.balance += amount
acc = SavingsAccount(100)
acc.deposit(50)
print(acc.balance)
D. class BankAccount:
def __init__(self, balance):
_balance = balance
class SavingsAccount(BankAccount):
def deposit(self, amount):
_balance += amount
acc = SavingsAccount(100)
acc.deposit(50)
print(_balance)
Solution
Step 1: Identify protected attribute usage
class BankAccount:
def __init__(self, balance):
self._balance = balance
class SavingsAccount(BankAccount):
def deposit(self, amount):
self._balance += amount
acc = SavingsAccount(100)
acc.deposit(50)
print(acc._balance) uses self._balance which is protected and accessible in subclass.