Last active
February 1, 2024 07:55
-
-
Save hibi-myzk/e130f89f1bf05e383db3a9dc9925459a to your computer and use it in GitHub Desktop.
Revisions
-
hibi-myzk revised this gist
Feb 1, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -533,7 +533,7 @@ "* 明示的な `return` が必要ない点に注意\n", "\n", "\n", "```ruby\n", "val = 0\n", "\n", "def test()\n", -
hibi-myzk revised this gist
Feb 1, 2024 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,14 +4,25 @@ "metadata": { "colab": { "provenance": [], "toc_visible": true, "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "<a href=\"https://colab.research.google.com/gist/hibi-myzk/e130f89f1bf05e383db3a9dc9925459a/without-global-variables.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" ] }, { "cell_type": "markdown", "source": [ -
hibi-myzk created this gist
Feb 1, 2024 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,556 @@ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "toc_visible": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Global variable" ], "metadata": { "id": "po8LRRYnLkuh" } }, { "cell_type": "code", "source": [ "global_history = []" ], "metadata": { "id": "LR2kPcVKK0Be" }, "execution_count": 71, "outputs": [] }, { "cell_type": "code", "source": [ "from datetime import datetime\n", "\n", "def stamp():\n", " current_datetime = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n", " global global_history\n", " global_history.append(current_datetime)\n", " return global_history" ], "metadata": { "id": "8YhYTZA9Lj74" }, "execution_count": 72, "outputs": [] }, { "cell_type": "code", "source": [ "stamp()\n", "stamp()\n", "stamp()\n", "\n", "global_history" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VhKO3QS4LqVA", "outputId": "9f6db241-75d3-49e4-8573-518c7e615289" }, "execution_count": 73, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['2024-02-01 02:16:27', '2024-02-01 02:16:27', '2024-02-01 02:16:27']" ] }, "metadata": {}, "execution_count": 73 } ] }, { "cell_type": "markdown", "source": [ "# Argument and return value" ], "metadata": { "id": "IQBmlbSbMKcR" } }, { "cell_type": "code", "source": [ "from datetime import datetime\n", "\n", "def stamp2(history):\n", " current_datetime = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n", " history.append(current_datetime)\n", " return history" ], "metadata": { "id": "HnlRwScJMJ7A" }, "execution_count": 74, "outputs": [] }, { "cell_type": "code", "source": [ "history = stamp2([])\n", "history = stamp2(history)\n", "history = stamp2(history)\n", "\n", "history" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ePTwzVEIMjeJ", "outputId": "8a68bd95-ca8e-43b6-82bb-0e908ff290b1" }, "execution_count": 75, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['2024-02-01 02:16:27', '2024-02-01 02:16:27', '2024-02-01 02:16:27']" ] }, "metadata": {}, "execution_count": 75 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "erK9nSMTOoEl" }, "execution_count": 75, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Closure\n", "\n", "※ [Clojure](https://clojure.org/) じゃないよ" ], "metadata": { "id": "-Zrq_rBZM8_q" } }, { "cell_type": "code", "source": [ "from datetime import datetime\n", "\n", "def generate_stamp3(history):\n", " def stamp3():\n", " current_datetime = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n", " history.append(current_datetime)\n", " return history\n", " return stamp3" ], "metadata": { "id": "IpCCyN9YM8RE" }, "execution_count": 76, "outputs": [] }, { "cell_type": "code", "source": [ "stamp3 = generate_stamp3([])\n", "\n", "stamp3()\n", "stamp3()\n", "history = stamp3()\n", "\n", "history" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "c4vbzVRyOGgO", "outputId": "23d962d6-74f7-4df5-f3e4-c1220635fcb2" }, "execution_count": 77, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['2024-02-01 02:16:27', '2024-02-01 02:16:27', '2024-02-01 02:16:27']" ] }, "metadata": {}, "execution_count": 77 } ] }, { "cell_type": "code", "source": [ "stamp3 = generate_stamp3([])\n", "\n", "print(stamp3())\n", "print(stamp3())\n", "print(stamp3())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6LaP45JhOr15", "outputId": "fc53012c-168e-440e-b6c4-b2f2d55b6d1c" }, "execution_count": 78, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['2024-02-01 02:16:27']\n", "['2024-02-01 02:16:27', '2024-02-01 02:16:27']\n", "['2024-02-01 02:16:27', '2024-02-01 02:16:27', '2024-02-01 02:16:27']\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Example" ], "metadata": { "id": "HXNoVXMwPFry" } }, { "cell_type": "code", "source": [ "def get_counter(count):\n", " c = count\n", " def counter():\n", " nonlocal c\n", " c += 1\n", " return c\n", " return counter\n", "\n", "counter = get_counter(0)\n", "\n", "print(counter())\n", "print(counter())\n", "print(counter())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XzwNybIuPFJL", "outputId": "8043c43e-71dc-420a-a444-23e913968e4e" }, "execution_count": 79, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "3\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Class" ], "metadata": { "id": "0MRtow_MXztg" } }, { "cell_type": "code", "source": [ "from datetime import datetime\n", "\n", "class MyHistory:\n", " def __init__(self):\n", " # private variable\n", " self.__history = []\n", "\n", " @property\n", " def history(self):\n", " return self.__history\n", "\n", " def stamp(self):\n", " current_datetime = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n", " self.__history.append(current_datetime)" ], "metadata": { "id": "0N6zFuivX6Vl" }, "execution_count": 80, "outputs": [] }, { "cell_type": "code", "source": [ "my_history = MyHistory()\n", "\n", "my_history.stamp()\n", "my_history.stamp()\n", "my_history.stamp()\n", "\n", "my_history.history" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2vzKgVYCX1Zc", "outputId": "8953bb8e-0bf5-4c31-f063-c8e53d61bd6c" }, "execution_count": 81, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['2024-02-01 02:16:27', '2024-02-01 02:16:27', '2024-02-01 02:16:27']" ] }, "metadata": {}, "execution_count": 81 } ] }, { "cell_type": "markdown", "source": [ "## Warning" ], "metadata": { "id": "QdAzJVJfaDxQ" } }, { "cell_type": "markdown", "source": [ "### Interface" ], "metadata": { "id": "RYNucGJKdDTp" } }, { "cell_type": "code", "source": [ "my_history = MyHistory()\n", "\n", "my_history.stamp()\n", "my_history.history.append('foo')\n", "my_history.stamp()\n", "\n", "my_history.history" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "C3oT0OAeaGdC", "outputId": "22875919-dcaa-49b3-bf64-91cde6cf6f1a" }, "execution_count": 82, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['2024-02-01 02:16:27', 'foo', '2024-02-01 02:16:27']" ] }, "metadata": {}, "execution_count": 82 } ] }, { "cell_type": "code", "source": [ "class MyHistory2:\n", " def __init__(self):\n", " # private variable\n", " self.__history = []\n", "\n", " def __iter__(self):\n", " return iter(self.__history)\n", "\n", " def stamp(self):\n", " current_datetime = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n", " self.__history.append(current_datetime)\n", " # add\n", " return len(self.__history)" ], "metadata": { "id": "O0NQD7MkbsGw" }, "execution_count": 83, "outputs": [] }, { "cell_type": "code", "source": [ "my_history2 = MyHistory2()\n", "\n", "my_history2.stamp()\n", "my_history2.stamp()\n", "my_history2.stamp()\n", "\n", "for t in my_history2:\n", " print(t)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7FC8iRyFb4TM", "outputId": "e90edd3f-ce4e-46cf-d478-8cba66b0ad9a" }, "execution_count": 84, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2024-02-01 02:16:27\n", "2024-02-01 02:16:27\n", "2024-02-01 02:16:27\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Return value" ], "metadata": { "id": "YG2r7YnUdNCB" } }, { "cell_type": "code", "source": [ "ret = my_history.stamp()\n", "\n", "ret == None" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2APEgHNJcJNE", "outputId": "64f1dd72-60cb-40f0-8458-529a56c0e3d3" }, "execution_count": 85, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 85 } ] }, { "cell_type": "code", "source": [ "ret = my_history2.stamp()\n", "\n", "ret" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Zk5bGIbYc5G5", "outputId": "959bcb8a-1b15-45fc-b409-e451ad2b43e7" }, "execution_count": 86, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": {}, "execution_count": 86 } ] }, { "cell_type": "markdown", "source": [ "Ruby の場合は…\n", "* `式` と `文` の違いに注意\n", "* 明示的な `return` が必要ない点に注意\n", "\n", "\n", "```\n", "val = 0\n", "\n", "def test()\n", " if true\n", " val = 1\n", " else\n", " val = -1\n", " end\n", "end\n", "\n", "ret = test()\n", "\n", "puts ret\n", "#=> 1\n", "\n", "val2 = if true\n", " val = 1\n", "else\n", " val = -1\n", "end\n", "\n", "puts val2\n", "#=> 1\n", "```" ], "metadata": { "id": "_bcIIf2CdvIY" } } ] }