{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# static method - @classmethod, @staticmethod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python에서는 static(정적) 메소드를 데코레이터로 정의 할 수 있음\n", "\n", "@staticmethod, @classmethod를 사용한다.\n", "\n", "이 둘의 차이점은 classmethod의 경우는 첫번째 인수가 클래스를 지정하는데 반해 staticmethod는 인수를 받지 않음!!!\n", "\n", "따라서 상속받는 경우 동작이 다름\n", "\n", "\n", "[참고 자료](http://blog.naver.com/parkjy76/30167615254)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:14:44.263772Z", "start_time": "2017-09-24T02:14:44.257700Z" }, "collapsed": true }, "outputs": [], "source": [ "class foo(object):\n", " name = 'foo'\n", "\n", " @staticmethod\n", " def get_name_static():\n", " print(foo.name)\n", "\n", " @classmethod\n", " def get_name_class(cls):\n", " print(cls.name)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:14:53.912627Z", "start_time": "2017-09-24T02:14:53.909729Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo\n" ] } ], "source": [ "foo.get_name_static()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:15:05.611764Z", "start_time": "2017-09-24T02:15:05.608498Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo\n" ] } ], "source": [ "foo.get_name_class()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 여기선 둘 다 동일" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:16:25.465263Z", "start_time": "2017-09-24T02:16:25.462492Z" }, "collapsed": true }, "outputs": [], "source": [ "class bar(foo):\n", " name = 'bar'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:16:26.714550Z", "start_time": "2017-09-24T02:16:26.711367Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo\n" ] } ], "source": [ "bar.get_name_static()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:16:45.421706Z", "start_time": "2017-09-24T02:16:45.418328Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar\n" ] } ], "source": [ "bar.get_name_class()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:20:45.729059Z", "start_time": "2017-09-24T02:20:45.724170Z" }, "collapsed": true }, "outputs": [], "source": [ "class foo(object):\n", " @classmethod\n", " def set_name(cls, name):\n", " cls.name = name\n", "\n", "class bar(foo):\n", " pass" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:21:05.630740Z", "start_time": "2017-09-24T02:21:05.624831Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo\n", "foo\n", "foo\n", "bar\n" ] } ], "source": [ "foo.set_name(\"foo\")\n", "print(foo.name)\n", "print(bar.name)\n", "\n", "bar.set_name(\"bar\")\n", "print(foo.name)\n", "print(bar.name)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:21:49.133794Z", "start_time": "2017-09-24T02:21:49.129108Z" }, "collapsed": true }, "outputs": [], "source": [ "class foo(object):\n", " @staticmethod\n", " def set_name(name):\n", " foo.name = name\n", "\n", "class bar(foo):\n", " pass" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2017-09-24T02:22:04.379206Z", "start_time": "2017-09-24T02:22:04.372761Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo\n", "foo\n", "bar\n", "bar\n" ] } ], "source": [ "foo.set_name(\"foo\")\n", "print(foo.name)\n", "print(bar.name)\n", "\n", "bar.set_name(\"bar\")\n", "print(foo.name)\n", "print(bar.name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }