Guest User

Untitled

a guest
Jan 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # private 네임 맹글링
  2. 파이썬은 클래스 정의 외부에서 볼 수 없도록 하는 속성에 대한 네이밍 컨벤션(naming convention)이 있다. 속성 이름 앞에 두 언더스코어(```__```)를 붙이면 된다.
  3.  
  4. ```python
  5. class Duck():
  6. def __init(self, input_name):
  7. self.__name = input_name
  8.  
  9. @property
  10. def name(self):
  11. print('inside the getter')
  12. return self.__name
  13. @name.setter
  14. def name(self, input_name):
  15. print('inside the setter')
  16. self.__name = input_name
  17. ```
  18. ```
  19. fowl = Duck('Howard')
  20. fowl.name # inside the getter 'Howard'
  21. fowl.name = 'Donald' # inside the setter
  22. fowl.name # inside the getter 'Donald'
  23. fowl.__name # error!
  24. ```
  25.  
  26. 이 네이밍 컨벤션은 속성을 `private`으로 만들지 않지만, 파이썬은 이 속성을 우연히 발견할 수 없도록 이름을 맹글링했다.
  27. ```
  28. fowl._Duck__name # 'Donald'
  29. ```
  30. inside the getter 를 출력하지 않았다! 하지만 의도적인 직접 접근은 충분히 막을 수 있다.
Add Comment
Please, Sign In to add comment