Python Basics: Object Oriented Programming (OOP)
Private Methods and Variables
Lesson 5 of 7 • 10 XP
Keep your place in this quest
Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.
In Object-Oriented Programming, not all data and methods are meant to be accessed directly from outside the class. Sometimes you want to hide internal details so they can only be used within the class itself.
In Python, you can signal that an attribute or method is private by starting its name with two underscores (__).
While this doesn’t make it truly impossible to access, it does activate a feature called name mangling, which changes the attribute’s name internally so it’s harder to access from the outside.
Why Use Private Members?
Private attributes and methods:
- Help protect data from being accidentally modified.
- Make it clear which parts of your class are internal and shouldn’t be relied on by outside code.
- Allow you to change internal implementation without breaking other code that uses your class.
IMPORTANT: Privacy in Python is by convention, not by force. Private members can still be accessed if someone really tries, but the intention is that you don’t.
Example: A Person Class
class Person:
def __init__(self, name, age):
self.name = name # Public attribute
self.__age = age # Private attribute
def print_name(self):
print("Name:", self.name)
def print_info(self):
self.print_name()
self.__print_age() # Calling private method from inside the class
def __print_age(self): # Private method
print("Age:", self.__age)
p = Person("John", 30)
p.print_name() # Works: Name: John
p.print_info() # Works: Name: John Age: 30
````
---
# What Happens if You Try to Access a Private Attribute?
If you try to access a private member directly from outside the class, you’ll get an `AttributeError`:
```python
p = Person("John", 30)
print(p.__age) # ❌ This will cause an error
Why?
Because Python automatically changes the name __age to _Person__age internally (name mangling). This helps avoid accidental access or conflicts with subclasses.
Accessing Private Data (If You Really Must)
Technically, you can still access private members by using their mangled name:
print(p._Person__age) # 30
But this breaks the whole idea of keeping things private — you should only do this in rare debugging situations, not in normal program logic.
Private Methods
Just like attributes, methods can be made private with double underscores.
In our example, __print_age() is private — you can’t call it from outside the class:
p.__print_age() # ❌ This will cause an error
But it works perfectly inside the class itself, as seen in print_info().
When to Use Private Members
Use private attributes and methods when:
- You want to prevent accidental misuse of internal parts of your class.
- You need to control how data is accessed or modified (often paired with getter and setter methods).
- You’re creating a clear separation between a class’s public API (things meant to be used from the outside) and its implementation details.
Wrapping Up
Private members in Python:
- Start with
__(double underscores). - Are intended for internal use only.
- Are still technically accessible through name mangling, but you should respect their privacy.
They’re a signal to other programmers (and to your future self!) about what’s safe to use and what’s an internal detail you might change later without warning.