Python Glossary Русский

all Python 3OOPterm

Encapsulation — _private, __mangled

encapsulation

A single _ is a convention (internal use). A double __ triggers name mangling (renaming to _Class__attr). Python has no truly private members.

Syntax

self._protected
self.__private

Name mangling exists so a subclass won't accidentally clobber an attribute, not to hide anything — you can still reach __attr as obj._Class__attr. It skips dunder names like __init__ (those also end in __), and a single _ enforces nothing at all, being pure convention.

Examples

class BankAccount:
    def __init__(self, balance):
        self._balance = balance  # 'protected'
    def get_balance(self):
        return self._balance
acc = BankAccount(100)
print(acc.get_balance())  # → 100
print(acc._balance)  # → 100 (можно, но нежелательно)
class Secret:
    def __init__(self):
        self.__secret = 42
    def reveal(self):
        return self.__secret
s = Secret()
print(s.reveal())  # → 42
try:
    s.__secret
except AttributeError as e:
    print(e)  # → 'Secret' object has no attribute '__secret'
print(s._Secret__secret)  # → 42 — имя искажено, но доступно
# В наследовании __attr не переопределяется случайно
class Base:
    def __init__(self):
        self.__x = 'base'
    def get_x(self):
        return self.__x
class Child(Base):
    def __init__(self):
        super().__init__()
        self.__x = 'child'  # → _Child__x, не трогает _Base__x
    def get_child_x(self):
        return self.__x
ch = Child()
print(ch.get_x())       # → base
print(ch.get_child_x()) # → child
class Config:
    _default = {'timeout': 30}
    def __init__(self, **kwargs):
        self._settings = {**self._default, **kwargs}
    def get(self, key):
        return self._settings.get(key)
cfg = Config(timeout=60)
print(cfg.get('timeout'))  # → 60
# Конвенция: _ означает 'не трогай'
class Transformer:
    def transform(self, data):
        data = self._preprocess(data)
        return self._process(data)
    def _preprocess(self, data):
        return [x.strip() for x in data]
    def _process(self, data):
        return [x.upper() for x in data]
t = Transformer()
print(t.transform(['  hello ', ' world ']))  # → ['HELLO','WORLD']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy