{"id":404,"date":"2025-03-18T15:33:32","date_gmt":"2025-03-18T15:33:32","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=404"},"modified":"2025-03-18T15:33:32","modified_gmt":"2025-03-18T15:33:32","slug":"%f0%9f%9a%80-decorator-trong-python-mo-rong-chuc-nang-ham-ma-khong-can-sua-code-%f0%9f%90%8d","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=404","title":{"rendered":"\ud83d\ude80 DECORATOR TRONG PYTHON \u2013 M\u1ede R\u1ed8NG CH\u1ee8C N\u0102NG H\u00c0M M\u00c0 KH\u00d4NG C\u1ea6N S\u1eecA CODE! \ud83d\udc0d"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"687\" height=\"455\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/decorator-python.png\" alt=\"\" class=\"wp-image-405\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/decorator-python.png 687w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/decorator-python-300x199.png 300w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/figure>\n<\/div>\n\n\n<p>B\u1ea1n \u0111\u00e3 bao gi\u1edd mu\u1ed1n <strong>th\u00eam t\u00ednh n\u0103ng cho m\u1ed9t h\u00e0m<\/strong> m\u00e0 <strong>kh\u00f4ng c\u1ea7n s\u1eeda \u0111\u1ed5i code g\u1ed1c<\/strong> ch\u01b0a? \ud83e\udd14 \u0110i\u1ec1u n\u00e0y r\u1ea5t quan tr\u1ecdng trong l\u1eadp tr\u00ecnh v\u00ec n\u1ebfu s\u1eeda code g\u1ed1c qu\u00e1 nhi\u1ec1u, ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 tr\u1edf n\u00ean kh\u00f3 b\u1ea3o tr\u00ec v\u00e0 d\u1ec5 g\u1eb7p l\u1ed7i.<\/p>\n\n\n\n<p>\ud83d\udd39 <strong>B\u1ea1n mu\u1ed1n \u0111o th\u1eddi gian th\u1ef1c thi c\u1ee7a m\u1ed9t h\u00e0m?<\/strong><br>\ud83d\udd39 <strong>B\u1ea1n mu\u1ed1n ki\u1ec3m tra quy\u1ec1n truy c\u1eadp tr\u01b0\u1edbc khi ch\u1ea1y m\u1ed9t h\u00e0m?<\/strong><br>\ud83d\udd39 <strong>B\u1ea1n mu\u1ed1n ghi log m\u1ed7i khi m\u1ed9t h\u00e0m \u0111\u01b0\u1ee3c g\u1ecdi?<\/strong><\/p>\n\n\n\n<p>T\u1ea5t c\u1ea3 nh\u1eefng \u0111i\u1ec1u tr\u00ean c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n <strong>m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng<\/strong> nh\u1edd <strong>Decorator<\/strong>! \ud83d\ude80<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>1. Decorator l\u00e0 g\u00ec?<\/strong><\/h2>\n\n\n\n<p><strong>Decorator trong Python l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt m\u1ea1nh m\u1ebd<\/strong> gi\u00fap b\u1ea1n <strong>m\u1edf r\u1ed9ng ho\u1eb7c ch\u1ec9nh s\u1eeda h\u00e0nh vi c\u1ee7a h\u00e0m m\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i n\u1ed9i dung b\u00ean trong n\u00f3<\/strong>.<\/p>\n\n\n\n<p>\ud83d\udca1 <strong>N\u00f3i \u0111\u01a1n gi\u1ea3n:<\/strong> Decorator l\u00e0 <strong>m\u1ed9t h\u00e0m nh\u1eadn m\u1ed9t h\u00e0m kh\u00e1c l\u00e0m tham s\u1ed1, sau \u0111\u00f3 th\u00eam ch\u1ee9c n\u0103ng cho h\u00e0m \u0111\u00f3 v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t h\u00e0m m\u1edbi<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udee0 <strong>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a Decorator<\/strong><\/h3>\n\n\n\n<p>1\ufe0f\u20e3 <strong>H\u00e0m g\u1ed1c v\u1eabn gi\u1eef nguy\u00ean.<\/strong><br>2\ufe0f\u20e3 <strong>Decorator nh\u1eadn h\u00e0m g\u1ed1c l\u00e0m tham s\u1ed1.<\/strong><br>3\ufe0f\u20e3 <strong>Decorator th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 thao t\u00e1c b\u1ed5 sung.<\/strong><br>4\ufe0f\u20e3 <strong>H\u00e0m m\u1edbi \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 v\u00e0 c\u00f3 th\u00eam ch\u1ee9c n\u0103ng m\u1edbi.<\/strong><\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n v\u1ec1 decorator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_decorator(func):\n    def wrapper():\n        print(\"Tr\u01b0\u1edbc khi g\u1ecdi h\u00e0m\")\n        func()\n        print(\"Sau khi g\u1ecdi h\u00e0m\")\n    return wrapper\n\n@my_decorator  # \u00c1p d\u1ee5ng decorator\ndef say_hello():\n    print(\"Xin ch\u00e0o!\")\n\nsay_hello()\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>K\u1ebft qu\u1ea3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tr\u01b0\u1edbc khi g\u1ecdi h\u00e0m  \nXin ch\u00e0o!  \nSau khi g\u1ecdi h\u00e0m  \n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>Decorator gi\u00fap b\u1ea1n th\u00eam ch\u1ee9c n\u0103ng v\u00e0o <code>say_hello()<\/code> m\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i code g\u1ed1c!<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>2. T\u1ea1i sao c\u1ea7n Decorator?<\/strong><\/h2>\n\n\n\n<p>Decorator gi\u00fap b\u1ea1n vi\u1ebft <strong>code g\u1ecdn h\u01a1n, d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n<\/strong> v\u00e0 gi\u00fap <strong>tr\u00e1nh l\u1eb7p code<\/strong>.<\/p>\n\n\n\n<p>\ud83d\udd39 <strong>Thay v\u00ec th\u00eam c\u00f9ng m\u1ed9t \u0111o\u1ea1n code v\u00e0o nhi\u1ec1u h\u00e0m<\/strong>, b\u1ea1n ch\u1ec9 c\u1ea7n vi\u1ebft m\u1ed9t decorator v\u00e0 \u00e1p d\u1ee5ng n\u00f3.<br>\ud83d\udd39 <strong>Decorator gi\u00fap code d\u1ec5 \u0111\u1ecdc h\u01a1n<\/strong> v\u00ec b\u1ea1n c\u00f3 th\u1ec3 hi\u1ec3u ngay r\u1eb1ng m\u1ed9t h\u00e0m \u0111\u00e3 \u0111\u01b0\u1ee3c m\u1edf r\u1ed9ng m\u00e0 kh\u00f4ng c\u1ea7n ki\u1ec3m tra t\u1eebng d\u00f2ng code b\u00ean trong.<br>\ud83d\udd39 <strong>D\u1ec5 d\u00e0ng b\u1eadt\/t\u1eaft c\u00e1c ch\u1ee9c n\u0103ng m\u1edf r\u1ed9ng<\/strong> b\u1eb1ng c\u00e1ch th\u00eam ho\u1eb7c g\u1ee1 b\u1ecf d\u00f2ng <code>@decorator_name<\/code>.<\/p>\n\n\n\n<p>\ud83d\udca1 <strong>\u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a Decorator:<\/strong><br>\u2705 <strong>Ghi log m\u1ed7i l\u1ea7n h\u00e0m \u0111\u01b0\u1ee3c g\u1ecdi<\/strong><br>\u2705 <strong>\u0110o th\u1eddi gian th\u1ef1c thi c\u1ee7a h\u00e0m<\/strong><br>\u2705 <strong>Ki\u1ec3m tra quy\u1ec1n truy c\u1eadp<\/strong><br>\u2705 <strong>X\u1eed l\u00fd l\u1ed7i t\u1ef1 \u0111\u1ed9ng<\/strong><br>\u2705 <strong>L\u01b0u cache k\u1ebft qu\u1ea3 \u0111\u1ec3 t\u0103ng t\u1ed1c<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>3. C\u00e1ch t\u1ea1o Decorator trong Python<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udee0 C\u1ea5u tr\u00fac c\u01a1 b\u1ea3n c\u1ee7a m\u1ed9t Decorator<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def decorator_name(func):\n    def wrapper(*args, **kwargs):\n        # Th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 thao t\u00e1c tr\u01b0\u1edbc khi g\u1ecdi h\u00e0m g\u1ed1c\n        result = func(*args, **kwargs)\n        # Th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 thao t\u00e1c sau khi g\u1ecdi h\u00e0m g\u1ed1c\n        return result\n    return wrapper\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>Gi\u1ea3i th\u00edch:<\/strong><br>\ud83d\udd39 <code>func<\/code> l\u00e0 h\u00e0m g\u1ed1c.<br>\ud83d\udd39 <code>wrapper<\/code> l\u00e0 h\u00e0m b\u1ecdc (h\u00e0m n\u00e0y th\u1ef1c hi\u1ec7n ch\u1ee9c n\u0103ng m\u1edf r\u1ed9ng).<br>\ud83d\udd39 <code>*args, **kwargs<\/code> gi\u00fap decorator c\u00f3 th\u1ec3 ho\u1ea1t \u0111\u1ed9ng v\u1edbi b\u1ea5t k\u1ef3 h\u00e0m n\u00e0o.<br>\ud83d\udd39 Cu\u1ed1i c\u00f9ng, <code>wrapper<\/code> \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 \u0111\u1ec3 thay th\u1ebf h\u00e0m g\u1ed1c.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>4. V\u00ed d\u1ee5 Th\u1ef1c T\u1ebf v\u1ec1 Decorator<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 1: Ghi log khi g\u1ecdi h\u00e0m<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def log_decorator(func):\n    def wrapper(*args, **kwargs):\n        print(f\"\u0110ang g\u1ecdi h\u00e0m {func.__name__} v\u1edbi c\u00e1c tham s\u1ed1 {args}, {kwargs}\")\n        result = func(*args, **kwargs)\n        print(f\"H\u00e0m {func.__name__} \u0111\u00e3 ch\u1ea1y xong\")\n        return result\n    return wrapper\n\n@log_decorator\ndef add(a, b):\n    return a + b\n\nprint(add(5, 10))\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>K\u1ebft qu\u1ea3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u0110ang g\u1ecdi h\u00e0m add v\u1edbi c\u00e1c tham s\u1ed1 (5, 10), {}\nH\u00e0m add \u0111\u00e3 ch\u1ea1y xong\n15\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 2: \u0110o th\u1eddi gian th\u1ef1c thi c\u1ee7a m\u1ed9t h\u00e0m<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\ndef timer_decorator(func):\n    def wrapper(*args, **kwargs):\n        start_time = time.time()\n        result = func(*args, **kwargs)\n        end_time = time.time()\n        print(f\"H\u00e0m {func.__name__} ch\u1ea1y trong {end_time - start_time:.4f} gi\u00e2y\")\n        return result\n    return wrapper\n\n@timer_decorator\ndef slow_function():\n    time.sleep(2)\n    print(\"H\u00e0m ch\u1ea1y xong!\")\n\nslow_function()\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>K\u1ebft qu\u1ea3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>H\u00e0m ch\u1ea1y xong!\nH\u00e0m slow_function ch\u1ea1y trong 2.0001 gi\u00e2y\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 3: Ki\u1ec3m tra quy\u1ec1n truy c\u1eadp<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def permission_required(func):\n    def wrapper(*args, **kwargs):\n        user = kwargs.get(\"user\", \"guest\")\n        if user != \"admin\":\n            print(\"B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n truy c\u1eadp!\")\n            return\n        return func(*args, **kwargs)\n    return wrapper\n\n@permission_required\ndef delete_file(file_name, user=\"guest\"):\n    print(f\"File {file_name} \u0111\u00e3 b\u1ecb x\u00f3a!\")\n\ndelete_file(\"data.txt\", user=\"admin\")  # Th\u00e0nh c\u00f4ng\ndelete_file(\"data.txt\", user=\"guest\")  # B\u1ecb ch\u1eb7n\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>K\u1ebft qu\u1ea3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File data.txt \u0111\u00e3 b\u1ecb x\u00f3a!\nB\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n truy c\u1eadp!\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>5. S\u1eed d\u1ee5ng nhi\u1ec1u Decorator c\u00f9ng l\u00fac<\/strong><\/h2>\n\n\n\n<p>Python cho ph\u00e9p b\u1ea1n \u00e1p d\u1ee5ng nhi\u1ec1u decorator l\u00ean c\u00f9ng m\u1ed9t h\u00e0m!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@log_decorator\n@timer_decorator\ndef example():\n    time.sleep(1)\n    print(\"V\u00ed d\u1ee5 ch\u1ea1y xong!\")\n\nexample()\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>K\u1ebft qu\u1ea3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u0110ang g\u1ecdi h\u00e0m example v\u1edbi c\u00e1c tham s\u1ed1 (), {}\nV\u00ed d\u1ee5 ch\u1ea1y xong!\nH\u00e0m example ch\u1ea1y trong 1.0002 gi\u00e2y\nH\u00e0m example \u0111\u00e3 ch\u1ea1y xong\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>L\u01b0u \u00fd:<\/strong> Decorator \u0111\u01b0\u1ee3c g\u1ecdi t\u1eeb d\u01b0\u1edbi l\u00ean tr\u00ean (tr\u01b0\u1edbc ti\u00ean <code>timer_decorator<\/code>, sau \u0111\u00f3 <code>log_decorator<\/code>).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>6. K\u1ebft lu\u1eadn<\/strong><\/h2>\n\n\n\n<p>\ud83d\udd39 <strong>Decorator l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap m\u1edf r\u1ed9ng ch\u1ee9c n\u0103ng c\u1ee7a h\u00e0m m\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i n\u1ed9i dung g\u1ed1c.<\/strong><br>\ud83d\udd39 <strong>Decorator gi\u00fap code s\u1ea1ch h\u01a1n, d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n, v\u00e0 tr\u00e1nh l\u1eb7p code.<\/strong><br>\ud83d\udd39 <strong>\u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i trong logging, ki\u1ec3m tra quy\u1ec1n, \u0111o th\u1eddi gian th\u1ef1c thi, caching, x\u1eed l\u00fd l\u1ed7i\u2026<\/strong><br>\ud83d\udd39 <strong>C\u00f3 th\u1ec3 k\u1ebft h\u1ee3p nhi\u1ec1u decorator \u0111\u1ec3 t\u1ea1o ra c\u00e1c h\u00e0m m\u1ea1nh m\u1ebd v\u00e0 linh ho\u1ea1t h\u01a1n.<\/strong><\/p>\n\n\n\n<p>\ud83c\udfaf <strong>H\u00e3y th\u1eed \u00e1p d\u1ee5ng Decorator ngay h\u00f4m nay \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a code c\u1ee7a b\u1ea1n!<\/strong> \ud83d\ude80<\/p>\n\n\n\n<p>B\u1ea1n \u0111\u00e3 t\u1eebng s\u1eed d\u1ee5ng Decorator ch\u01b0a? H\u00e3y chia s\u1ebb c\u1ea3m nh\u1eadn c\u1ee7a b\u1ea1n trong ph\u1ea7n b\u00ecnh lu\u1eadn! \u2b07\ufe0f\ud83d\udcac<\/p>\n\n\n\n<p>#Python #Decorator #LapTrinhPython #PythonTips #CodeOptimization<\/p>\n","protected":false},"excerpt":{"rendered":"<p>B\u1ea1n \u0111\u00e3 bao gi\u1edd mu\u1ed1n th\u00eam t\u00ednh n\u0103ng cho m\u1ed9t h\u00e0m m\u00e0 kh\u00f4ng c\u1ea7n s\u1eeda \u0111\u1ed5i code g\u1ed1c ch\u01b0a? \ud83e\udd14 \u0110i\u1ec1u n\u00e0y r\u1ea5t quan tr\u1ecdng trong l\u1eadp tr\u00ecnh v\u00ec n\u1ebfu s\u1eeda code&#8230;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":406,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions\/406"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}