{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. 복잡한 식을 쓰는 대신 도우미 함수를 작성하라" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from urllib.parse import parse_qs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "my_values = parse_qs('빨강=5&파랑=0&초록=', keep_blank_values=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'빨강': ['5'], '파랑': ['0'], '초록': ['']}\n" ] } ], "source": [ "print(repr(my_values))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "결과 딕셔너리에 get 메서드를 사용하면 상황에 따라 다른 값 반환" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "빨강: ['5']\n" ] } ], "source": [ "print('빨강:', my_values.get('빨강'))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "초록: ['']\n" ] } ], "source": [ "print('초록:', my_values.get('초록'))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "투명도: None\n" ] } ], "source": [ "print('투명도:', my_values.get('투명도'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "파라미터가 없거나 비어 있을 경우 0이 디폴트 값으로 대입하는 방법" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "red = my_values.get('빨강', [''])[0] or 0\n", "green = my_values.get('초록', [''])[0] or 0\n", "opacity = my_values.get('투명도', [''])[0] or 0" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "빨강: '5'\n", "초록: 0\n", "투명도: 0\n" ] } ], "source": [ "print(f'빨강: {red!r}')\n", "print(f'초록: {green!r}')\n", "print(f'투명도: {opacity!r}')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "빨강: 5\n" ] } ], "source": [ "print(f'빨강: {red}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "이 식은 읽기 어려운 데다 우리가 원하는 모든 기능을 제공하지도 못한다.\n", "\n", "우리는 모든 파라미터 값을 정수로 변환해서 즉시 수식에 활용하길 바란다.\n", "\n", "그렇게 하려면 각 식을 int 내장 함수로 감싸서 문자열을 정수로 구문분석 해야한다." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "red = int(my_values.get('빨강', [''])[0] or 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "코드 이해하기 쉽지 않음" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "red_str = my_values.get('빨강', [''])\n", "red = int(red_str[0]) if red_str[0] else 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "이 코드가 더 좋지만 덜 명확하다" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "green_str = my_values.get('초록', [''])\n", "if green_str[0]:\n", " green = int(green_str[0])\n", "else:\n", " green = 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "이게 더 명확하며 보기 좋다.\n", "\n", "이 로직을 반복 적용하려면 (두 세번이라도) 꼭 도우미 함수를 작성해야한다." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def get_first_int(values, key, default=0):\n", " found = values.get(key, [''])\n", " if found[0]:\n", " return int(found[0])\n", " return default" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "green = get_first_int(my_values, '초록')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "훨씬 명확하고 간편하다" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "식이 복잡해지기 시작하면 바로 식을 더 작은 조각으로 나눠서 로직을 도우미 함수로 옮길지 고려해야 한다.\n", "\n", "가독성이 가치가 있다!!\n", "\n", "복잡한 식을 표현할 수 있는 파이썬의 함축적인 문법이 이런 지저분한 코드를 만들어내지 않도록 하라.\n", "\n", "'반복하지 말라(Don't Repeat Yourself)'는 뜻의 DRY 원칙을 따르라." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 기억해야 할 내용\n", "\n", "- 파이썬 문법을 사용하면 아주 복잡하고 읽기 어려운 한 줄짜리 식을 쉽게 작성할 수 있다.\n", "- 복잡한 식을 도우미 함수로 옮겨라. 특히 같은 로직을 반복해 사용할 때는 도우미 함수를 꼭 사용하라.\n", "- 불(boolean) 연산자 or나 and를 식에 사용하는 것 보다 if/else 식을 쓰는 편이 더 가독성이 좋다.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }