Python supports the concept of encapsulation, which helps protect data inside a program. One important part of this concept is private variables.
In this post, we will learn what private variables are, how they work in Python, and how to use them with examples.
🧠 What are Private Variables in Python?
A private variable is a variable that is intended to be used only inside a class and should not be accessed directly from outside the class.
👉 In simple words:
- Private variables = hidden data inside a class
- Used to protect data from direct access
⚙️ How Python Defines Private Variables
In Python, private variables are created by adding double underscores __ before the variable name.
Example:
class Person:
def __init__(self):
self.__name = "Python"
p = Person()
👉 __name is a private variable.
🔒 1. Accessing Private Variables (Direct Access)
If you try to access a private variable directly, Python will show an error.
Example:
class Person:
def __init__(self):
self.__name = "Python"
p = Person()
print(p.__name)
❌ Output:
AttributeError: 'Person' object has no attribute '__name'
👉 This shows the variable is protected.
🧠 2. Accessing Private Variables Using Methods
Private variables can be accessed using public methods (getter methods).
Example:
class Person:
def __init__(self):
self.__name = "Python"
def get_name(self):
return self.__name
p = Person()
print(p.get_name())
✅ Output:
Python
⚙️ 3. Modifying Private Variables
You can also modify private variables using setter methods.
Example:
class Person:
def __init__(self):
self.__name = "Python"
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
p = Person()
p.set_name("AI Python")
print(p.get_name())
🔐 4. Why Python Uses Private Variables
Private variables help in data protection and encapsulation.
Benefits:
- Prevent accidental changes
- Improve code security
- Maintain data integrity
- Better control inside classes
⚠️ 5. Name Mangling in Python
Python does not fully block private variables. Instead, it changes their name internally (called name mangling).
Example:
class Person:
def __init__(self):
self.__name = "Python"
p = Person()
print(p._Person__name)
⚠️ Output:
Python
👉 But this is NOT recommended in real programming.
🧪 6. Example Program with Private Variables
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
account = BankAccount(1000)
account.deposit(500)
print("Balance:", account.get_balance())
⚖️ 7. Public vs Private Variables
| Feature | Public Variable | Private Variable |
|---|---|---|
| Access | Outside class allowed | Inside class only |
| Security | Low | High |
| Syntax | name | __name |
| Usage | Simple data | Protected data |
🚀 8. Real-Life Example
Think of a bank account:
- Balance = private variable 💰
- You cannot directly change it
- You can only use deposit/withdraw methods
👉 This is exactly how private variables work in Python.
🧾 Conclusion
Private variables in Python are used to protect data inside classes and ensure safe access through methods. They are an important part of object-oriented programming and encapsulation.
💡 Final Thought
If you want to build secure and professional applications in Python, understanding private variables is essential.


0 Comments