EunGyeongKim

[python] _ underscore의 의미 본문

Language/Python

[python] _ underscore의 의미

EunGyeongKim 2024. 2. 6. 13:23

  • 클래스 내에서 쓰는 변수
  • 변수가 private라는 의미

 

__ 의미

  • _클래스이름__함수
    • 클래스 전용 인스턴스 및 클래스 변수, 메서드, 전역에 저장된 변수, 인스턴스에 저장된 변수를 정의

 

__함수__ 의 의미

  • 일반적으로 내장 메소드 또는 변수용으로 예약

 

 

 

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

 

 

Comments