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 private attribute in Python?
A private attribute is a variable inside a class that is meant to be hidden from outside access. It usually starts with two underscores (e.g., <code>__name</code>) to indicate it should not be accessed directly from outside the class.
Click to reveal answer
beginner
How do you define a private attribute in a Python class?
You define a private attribute by starting its name with two underscores, like self.__age = 25. This tells Python to mangle the name to make it harder to access from outside.
Click to reveal answer
beginner
Why use private attributes in a class?
Private attributes help protect data inside an object. They prevent accidental changes or misuse from outside the class, helping keep the object's state safe and controlled.
Click to reveal answer
intermediate
What happens if you try to access a private attribute directly from outside the class?
Python will raise an AttributeError because the attribute name is changed internally (name mangling). You cannot access it by its original name directly.
Click to reveal answer
intermediate
How can you access a private attribute from outside the class if needed?
You can access it using the mangled name, which is <code>_ClassName__attribute</code>. For example, if the class is <code>Person</code> and attribute is <code>__age</code>, access it as <code>instance._Person__age</code>. But this is not recommended.
Click to reveal answer
How do you declare a private attribute in a Python class?
AStart the attribute name with one underscore (_)
BStart the attribute name with two underscores (__)
CUse the keyword 'private' before the attribute
DUse capital letters for the attribute name
✗ Incorrect
Private attributes start with two underscores to trigger name mangling and hide them from outside access.
What error do you get if you try to access a private attribute directly?
AAttributeError
BSyntaxError
CTypeError
DNameError
✗ Incorrect
Trying to access a private attribute directly raises AttributeError because the name is mangled internally.
Which of these is the correct way to access a private attribute from outside the class (not recommended)?
Ainstance._ClassName__attribute
Binstance.__attribute
Cinstance.attribute
Dinstance.private_attribute
✗ Incorrect
Private attributes are accessed outside the class using the mangled name: _ClassName__attribute.
Why should you use private attributes?
ATo make the code run faster
BTo make the attribute public
CTo allow anyone to change the attribute
DTo protect data from accidental changes
✗ Incorrect
Private attributes protect data inside the object from accidental or unauthorized changes.
What is name mangling in Python?
AConverting attribute names to uppercase
BRenaming variables to shorter names
CChanging the attribute name to include the class name internally
DDeleting private attributes automatically
✗ Incorrect
Name mangling changes private attribute names by adding the class name prefix to make them harder to access directly.
Explain what private attributes are and why they are useful in Python classes.
Think about how you keep some things private in real life and how that idea applies to programming.
You got /4 concepts.
Describe how Python handles private attributes internally and how you can access them if needed.
Imagine a secret code that changes the name so only the class knows it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using private attributes (starting with __) in a Python class?
easy
A. To speed up the program execution
B. To hide the attribute from outside the class and protect it
C. To make the attribute accessible everywhere
D. To make the attribute a global variable
Solution
Step 1: Understand private attribute naming
Private attributes start with double underscores to hide them from outside access.
Step 2: Purpose of hiding attributes
This protects the data inside the object from accidental or unauthorized changes.
Final Answer:
To hide the attribute from outside the class and protect it -> Option B
Quick Check:
Private attributes = data protection [OK]
Hint: Private attributes start with __ to hide data inside class [OK]
Common Mistakes:
Thinking private means accessible everywhere
Confusing private with global variables
Believing private speeds up code
2. Which of the following is the correct way to define a private attribute named age in a Python class?
easy
A. self.___age
B. self.age
C. self._age
D. self.__age
Solution
Step 1: Identify private attribute syntax
Private attributes start with exactly two underscores, like __age.
Step 2: Check options
self.__age uses self.__age, which is correct. self.age is public, self._age is protected (single underscore), self.___age has three underscores which is invalid.
Final Answer:
self.__age -> Option D
Quick Check:
Private attribute = double underscore [OK]
Hint: Private attribute = double underscore before name [OK]
Common Mistakes:
Using single underscore instead of double
Adding too many underscores
Forgetting underscores
3. What will be the output of this code?
class Person:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
p = Person('Anna')
print(p.get_name())
print(p.__name)
medium
A. Anna\nAnna
B. AttributeError\nAnna
C. Anna\nAttributeError
D. AttributeError\nAttributeError
Solution
Step 1: Understand private attribute access
The attribute __name is private and cannot be accessed directly outside the class.
Step 2: Check print statements
Calling p.get_name() returns 'Anna' correctly. But p.__name causes AttributeError because it's private.
Final Answer:
Anna\nAttributeError -> Option C
Quick Check:
Private attribute direct access = AttributeError [OK]
Hint: Private attributes cause error if accessed directly outside class [OK]
Common Mistakes:
Expecting direct access to private attribute
Ignoring AttributeError on private access
Confusing method call with attribute access
4. Find the error in this code and fix it:
class Car:
def __init__(self, model):
self.__model = model
c = Car('Tesla')
print(c.__model)
medium
A. Add a method inside class to return __model
B. No error, code runs fine
C. Remove underscores from __model
D. Change __model to _model to fix access
Solution
Step 1: Identify the error
Accessing c.__model outside the class causes AttributeError because __model is private.
Step 2: Fix by adding a getter method
Adding a method inside the class to return self.__model allows safe access.
Final Answer:
Add a method inside class to return __model -> Option A
Quick Check:
Private attribute access needs class method [OK]
Hint: Use class method to access private attributes outside class [OK]
Common Mistakes:
Trying to access private attribute directly
Removing underscores breaks privacy
Ignoring need for getter method
5. You want to store a private attribute __balance in a BankAccount class and allow safe updating only through a method that adds money. Which code snippet correctly implements this?
hard
A. class BankAccount:
def __init__(self):
self.__balance = 0
def add_money(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
B. class BankAccount:
def __init__(self):
self._balance = 0
def add_money(self, amount):
self._balance += amount
def get_balance(self):
return self._balance
C. class BankAccount:
def __init__(self):
self.balance = 0
def add_money(self, amount):
self.balance += amount
def get_balance(self):
return self.balance
D. class BankAccount:
def __init__(self):
self.__balance = 0
def add_money(self, amount):
self.balance += amount
def get_balance(self):
return self.__balance