https://docs.python.org/3/tutorial/classes.html#private-variables

파이썬엔 진짜 의미의 private은 없고, 두 가지 관례/기능만 있다.

  • _name: 비공개 의도라는 신호일 뿐 접근 가능. API 변경 가능성을 암시.
  • __name: 클래스 정의 안에서만 이름 맹글링되어 _ClassName__name으로 저장, 서
    브클래스와의 이름 충돌을 피우는 용도. 여전히 obj._ClassName__name으로 접
    근/수정 가능해서 강제 숨김은 아님.

이름 맹글링 예:

class Mapping:
      def __init__(self, it):
          self.items = []
          self.__update(it)   # 실제로는 _Mapping__update 호출
 
      def update(self, it):
          self.items.extend(it)
 
      __update = update      # _Mapping__update로 맹글링되어 사본 보관
 
  class Sub(Mapping):
      def update(self, keys, vals):
          self.items.extend(zip(keys, vals))  # 새 시그니처

Sub가 update를 오버라이드해도 Mapping.__init__에서 호출하는 __update는 맹글
링된 _Mapping__update를 가리켜 안전하게 동작합니다.

← python 3.14으로