python 내부 함수로 property( fget, fset, fdel, doc ) 가 있습니다. argument 는 모두 optional 입니다.
fget is function to get value of the attribute
- fget is function to get value of the attribute
- fset is function to set value of the attribute
- fdel is function to delete the attribute
- doc is a string ( like a comment )
property 함수로 생성된 객체에 관해 값을 반환하는 코드가 실행되면 '자동'으로 fget 함수가 실행되며, 해당 객체에 값을 할당하는 코드가 실행되면 '자동'으로 fset 함수가 실행됩니다.
그러나 굳이 fget, fset 을 정의하지 않아도 fget, fset 기능이 있는 property 로 생성된 객체를 이용할 수 있습니다. 이걸 가능하게 하는 것이 바로 @property 와 @property.setter 입니다. fget, fset 의 기능은 다음과 같이 @property 와 @property.setter 로 사용될 수 있습니다.
fget 의 경우
@property
def property_obj_name(self):
return self._property_obj_name
fset 의 경우
@property.setter
def property_obj_name(self, value):
self._property_obj_name = value
이렇게 @property 와 @property.setter 를 이용하면 fget, fset 을 따로 정의하지 않고 더 직관적으로 getter, setter 를 이용할 수 있습니다.
다음의 웹페이지를 참고했습니다. 아래 글에는 예시까지 있어 이해하는데 더 도움이 될 것이라고 생각합니다.
https://www.programiz.com/python-programming/property
Python @property Decorator (With Examples)
Python @property decorator In this tutorial, you will learn about Python @property decorator; a pythonic way to use getters and setters in object-oriented programming. Python programming provides us with a built-in @property decorator which makes usage of
www.programiz.com