{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 19. 함수가 여러 값을 반환하는 경우 절대로 네 값 이상을 언패킹하지 말라" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def get_stats(numbers):\n", " minimum = min(numbers)\n", " maximum = max(numbers)\n", " return minimum, maximum" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "length = [63, 73, 72, 60, 67, 66, 71, 61, 72, 70]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "minimum, maximum = get_stats(length)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "minimum" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "73" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def get_avg_ratio(numbers):\n", " avg = sum(numbers) / len(numbers)\n", " scaled = [x / avg for x in numbers]\n", " scaled.sort(reverse=True)\n", " return scaled" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "longest, *middle, shortest = get_avg_ratio(length)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0814814814814815" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "longest" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.0666666666666667,\n", " 1.0666666666666667,\n", " 1.0518518518518518,\n", " 1.037037037037037,\n", " 0.9925925925925926,\n", " 0.9777777777777777,\n", " 0.9333333333333333,\n", " 0.9037037037037037]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "middle" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8888888888888888" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shortest" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def get_stats(numbers):\n", " minimum = min(numbers)\n", " maximum = max(numbers)\n", " count = len(numbers)\n", " average = sum(numbers) / count\n", " sorted_numbers = sorted(numbers)\n", " middle = count // 2\n", " if count % 2 == 0:\n", " lower = sorted_numbers[middle - 1]\n", " upper = sorted_numbers[middle]\n", " median = (lower + upper) / 2\n", " else:\n", " median = sorted_numbers[middle]\n", "\n", " return minimum, maximum, average, median, count" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "minimum, maximum, average, median, count = get_stats(length)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "minimum" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "73" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "67.5" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "average" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "68.5" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "median" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 기억해야 할 내용\n", "- 함수가 여러 값을 반환하기 위해 값들을 튜플에 넣어서 반환하고, 호출하는 쪽에서는 파이썬 언패킹 구문을 쓸 수 있다.\n", "- 함수가 반환한 여러 값을, 모든 값을 처리하는 별표 식을 사용해 언패킹할 수도 있다.\n", "- 언패킹 구문에 변수가 네개 이상 나오면 실수하기 쉬우므로 변수를 네 개 이상 사용하면 안 된다. 대식 작은 클래스를 반환하거나 namedtuple 인스턴스를 반환하라" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }