Bird
Raised Fist0
Pythonprogramming~10 mins

Protected attributes in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Protected attributes
Define class with _protected attribute
Create object instance
Access _protected attribute inside class
Access _protected attribute outside class
Modify _protected attribute
Program continues
This flow shows how a protected attribute (with a single underscore) is defined and accessed inside and outside a class, highlighting that access outside is allowed but discouraged.
Execution Sample
Python
class Car:
    def __init__(self):
        self._speed = 0

car = Car()
print(car._speed)
Defines a class with a protected attribute _speed, creates an object, and prints the protected attribute.
Execution Table
StepActionVariable/AttributeValueOutput
1Define class Car with __init___speedDefined as protected attribute
2Create instance carcarCar object created
3Call __init__ for carcar._speed0
4Print car._speedcar._speed00
5End of program
💡 Program ends after printing the protected attribute value 0
Variable Tracker
VariableStartAfter Step 2After Step 3Final
carundefinedCar object createdCar object with _speed=0Car object with _speed=0
car._speedundefinedundefined00
Key Moments - 2 Insights
Why can we access the _speed attribute outside the class even though it is 'protected'?
In Python, a single underscore prefix is a convention to indicate 'protected', but it does not prevent access. As shown in step 4 of the execution_table, accessing car._speed outside the class works but is discouraged.
What happens if we try to modify the _speed attribute outside the class?
You can modify it freely because Python does not enforce protection. The underscore is just a hint to programmers. This is implied by the convention and the execution flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of car._speed when printed?
AUndefined
B0
CError
DNone
💡 Hint
Check the 'Value' and 'Output' columns at step 4 in the execution_table.
At which step is the protected attribute _speed first assigned a value?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Variable/Attribute' and 'Value' columns in the execution_table to see when car._speed is set.
If we rename _speed to speed (no underscore), how would the access outside the class change?
AIt would cause an error when accessed outside
BIt would become private and inaccessible
CIt would still be accessible without any warning
DIt would require special syntax to access
💡 Hint
Recall that underscore is a convention; removing it does not restrict access (see key_moments).
Concept Snapshot
Protected attributes in Python start with a single underscore (_).
This is a convention to signal 'internal use' but does not prevent access.
You can access and modify them outside the class, but it's discouraged.
Use them to indicate 'please don't touch this' to other programmers.
Full Transcript
This visual execution shows how protected attributes work in Python. A class Car defines a protected attribute _speed with a single underscore. When an object car is created, _speed is set to 0 inside the constructor. Accessing car._speed outside the class prints 0 without error. This is because Python's single underscore is a naming convention only, not an access restriction. The execution table traces each step: defining the class, creating the object, initializing _speed, and printing it. The variable tracker shows how car and car._speed change over time. Key moments clarify that protected attributes can be accessed and modified outside the class, but it is discouraged by convention. The quiz tests understanding of when _speed is set, its value when printed, and the effect of removing the underscore. The snapshot summarizes the concept simply for quick reference.

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

  1. 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.
  2. Step 2: Differentiate from private and public

    Private attributes use double underscores, public have no underscore, and constants are uppercase without underscores.
  3. Final Answer:

    It is a protected attribute meant for internal use within the class and subclasses. -> Option B
  4. 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

  1. Step 1: Identify protected attribute syntax

    Protected attributes start with a single underscore, so self._count is correct.
  2. Step 2: Check other options

    self.count is public, self.__count is private, and count = 0 is a local variable, not an attribute.
  3. Final Answer:

    self._count = 0 -> Option A
  4. 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
  • Defining attribute without self inside methods
3. What will be the output of this code?
class MyClass:
    def __init__(self):
        self._value = 10

obj = MyClass()
print(obj._value)
medium
A. AttributeError
B. None
C. 10
D. SyntaxError

Solution

  1. Step 1: Understand protected attribute access

    Protected attributes can be accessed outside the class, though it is discouraged.
  2. Step 2: Check code behavior

    The attribute _value is set to 10 and printed directly, so output is 10.
  3. Final Answer:

    10 -> Option C
  4. 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

  1. Step 1: Check attribute names in Parent and Child

    Parent defines self._data, but Child tries to print self.data, which does not exist.
  2. Step 2: Understand error type

    Accessing a non-existent attribute causes AttributeError at runtime.
  3. Final Answer:

    AttributeError because self.data does not exist -> Option D
  4. 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

  1. 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.
  2. Step 2: Check attribute access and update

    Subclass method deposit updates self._balance correctly. Printing acc._balance shows updated value.
  3. Step 3: Analyze other options

    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 double underscore (private), so subclass cannot access __balance directly. 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 public attribute. 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) uses local variable _balance not attached to self, causing errors.
  4. Final Answer:

    Uses self._balance (single underscore) in parent and subclass. -> Option A
  5. Quick Check:

    Protected attribute with single underscore and subclass access = 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) [OK]
Hint: Use single underscore and self. for protected attributes [OK]
Common Mistakes:
  • Using double underscore for protected attribute
  • Not using self. to define attributes
  • Using local variables instead of instance attributes