Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # private 네임 맹글링
- 파이썬은 클래스 정의 외부에서 볼 수 없도록 하는 속성에 대한 네이밍 컨벤션(naming convention)이 있다. 속성 이름 앞에 두 언더스코어(```__```)를 붙이면 된다.
- ```python
- class Duck():
- def __init(self, input_name):
- self.__name = input_name
- @property
- def name(self):
- print('inside the getter')
- return self.__name
- @name.setter
- def name(self, input_name):
- print('inside the setter')
- self.__name = input_name
- ```
- ```
- fowl = Duck('Howard')
- fowl.name # inside the getter 'Howard'
- fowl.name = 'Donald' # inside the setter
- fowl.name # inside the getter 'Donald'
- fowl.__name # error!
- ```
- 이 네이밍 컨벤션은 속성을 `private`으로 만들지 않지만, 파이썬은 이 속성을 우연히 발견할 수 없도록 이름을 맹글링했다.
- ```
- fowl._Duck__name # 'Donald'
- ```
- inside the getter 를 출력하지 않았다! 하지만 의도적인 직접 접근은 충분히 막을 수 있다.
Add Comment
Please, Sign In to add comment