Private attributes help keep some parts of an object hidden so others can't change them by mistake.
Private attributes in Python
class ClassName: def __init__(self): self.__private_attribute = None
Private attributes start with two underscores __.
This makes Python change the name internally to avoid easy access from outside.
__speed inside the Car class.class Car: def __init__(self, speed): self.__speed = speed
__name is accessed safely using a method get_name.class Person: def __init__(self, name): self.__name = name def get_name(self): return self.__name
This program shows a private attribute __balance in a BankAccount class. It can only be changed by methods inside the class. Trying to access it directly causes an error.
class BankAccount: def __init__(self, balance): self.__balance = balance # private attribute def deposit(self, amount): if amount > 0: self.__balance += amount def get_balance(self): return self.__balance account = BankAccount(100) print(account.get_balance()) # prints 100 account.deposit(50) print(account.get_balance()) # prints 150 # Trying to access private attribute directly try: print(account.__balance) except AttributeError as e: print(e)
Private attributes are not truly hidden but renamed internally (name mangling).
You can still access them using _ClassName__attribute but it is not recommended.
Use private attributes to protect important data and keep your code clean.
Private attributes start with two underscores __ to hide them.
They help protect data inside objects from outside changes.
Access private attributes using methods inside the class.
Practice
__) in a Python class?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 BQuick Check:
Private attributes = data protection [OK]
- Thinking private means accessible everywhere
- Confusing private with global variables
- Believing private speeds up code
age in a Python class?Solution
Step 1: Identify private attribute syntax
Private attributes start with exactly two underscores, like__age.Step 2: Check options
self.__age usesself.__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 DQuick Check:
Private attribute = double underscore [OK]
- Using single underscore instead of double
- Adding too many underscores
- Forgetting underscores
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)Solution
Step 1: Understand private attribute access
The attribute__nameis private and cannot be accessed directly outside the class.Step 2: Check print statements
Callingp.get_name()returns 'Anna' correctly. Butp.__namecauses AttributeError because it's private.Final Answer:
Anna\nAttributeError -> Option CQuick Check:
Private attribute direct access = AttributeError [OK]
- Expecting direct access to private attribute
- Ignoring AttributeError on private access
- Confusing method call with attribute access
class Car:
def __init__(self, model):
self.__model = model
c = Car('Tesla')
print(c.__model)Solution
Step 1: Identify the error
Accessingc.__modeloutside the class causes AttributeError because__modelis private.Step 2: Fix by adding a getter method
Adding a method inside the class to returnself.__modelallows safe access.Final Answer:
Add a method inside class to return __model -> Option AQuick Check:
Private attribute access needs class method [OK]
- Trying to access private attribute directly
- Removing underscores breaks privacy
- Ignoring need for getter method
__balance in a BankAccount class and allow safe updating only through a method that adds money. Which code snippet correctly implements this?Solution
Step 1: Check private attribute usage
class BankAccount: def __init__(self): self.__balance = 0 def add_money(self, amount): self.__balance += amount def get_balance(self): return self.__balance usesself.__balanceconsistently and privately.Step 2: Verify method updates and access
Theadd_moneymethod safely updates__balance, andget_balancereturns it correctly.Final Answer:
The code using self.__balance consistently in all methods -> Option AQuick Check:
Private attribute updated only inside class methods [OK]
- Updating private attribute outside class
- Mixing private and public attribute names
- Not providing method to access private data
Ready to prove it?
Pass the test and this pattern moves unknown
