brunch

개체지향 - 프로퍼티, 데코레이터

getter, setter

by 내가 사는 세상

목차

1. 프로퍼티

2. 데코레이터 #프로퍼티를 지정하는 또다른 방법




1. 프로퍼티


프로퍼티라는 개념은 getter와 setter를 매번 쓰는게 귀찮아서 나타났다.




2. 데코레이터 #프로퍼티를 지정하는 또다른 방법


데코레이터는 함수에 추가적인 동작을 붙이는 것이다. 말 그대로 특정 함수를 꾸미는(decorate) 것이다. 구체적으로는 해당 함수를 감싸서 호출 전후에 원하는 작업이 이루어지도록 한다. 예시를 보자.





from rest_framework.response import Response

from rest_framework import status


def custom_decorator(view_func):

def wrapped_view(request, *args, **kwargs):

# 여기에 원하는 동작을 추가

if some_condition:

return Response(

{"error": "Some condition not met"},

status=status.HTTP_400_BAD_REQUEST

)

return view_func(request, *args, **kwargs)

return wrapped_view


@custom_decorator

def custom_decorated_view(request):

# custom_decorator에서 정의한 조건을 충족할 때만 실행

keyword
매거진의 이전글개체지향 - 스페셜 멤버함수