Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Python

Encapsulation in Python

Protect internal state using naming conventions and properties.

Encapsulation in Python — a practical guide to Python encapsulation with clear examples you can reuse in real projects.

Python Tutorial Series (49/95). Prefer one article? Read the complete Python tutorial.

Short description

Python uses conventions (`_protected`, `__private`) and `@property` instead of strict access modifiers.

Property example

class Account:
    def __init__(self, balance):
        self._balance = balance

    @property
    def balance(self):
        return self._balance

print(Account(500).balance)

Leave a reply

Your email address will not be published. Required fields are marked *