파이썬
[python] isinstance 함수
글을 쓰는 개발자
2021. 8. 4. 15:13
반응형
isinstance를 궁금하게 된 계기는 drf views.py를 조사하게 된 것이 계기였다.
def exception_handler(exc, context):
"""
Returns the response that should be used for any given exception.
By default we handle the REST framework `APIException`, and also
Django's built-in `Http404` and `PermissionDenied` exceptions.
Any unhandled exceptions may return `None`, which will cause a 500 error
to be raised.
"""
if isinstance(exc, Http404):
exc = exceptions.NotFound()
elif isinstance(exc, PermissionDenied):
exc = exceptions.PermissionDenied()
if isinstance(exc, exceptions.APIException):
headers = {}
if getattr(exc, 'auth_header', None):
headers['WWW-Authenticate'] = exc.auth_header
if getattr(exc, 'wait', None):
headers['Retry-After'] = '%d' % exc.wait
if isinstance(exc.detail, (list, dict)):
data = exc.detail
else:
data = {'detail': exc.detail}
set_rollback()
return Response(data, status=exc.status_code, headers=headers)
return None
views의 한 부분인데 이 때 isinstance가 무엇인 지 궁금하게 되었다.
찾아보니 별 거 없었고 해당 인스턴스가 이러한 타입이 맞는 지 확인하는 메소드 였다.
반응형