You are on page 1of 5

{

"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"##假設你的報表程式產生中,有 30 支含商業𨗴輯的函式;\n",
"##在某個陰雨綿綿的星期一早晨,老闆走到你辦公室裡,說:早呀,記得那些 TPS 報表嗎?我需要你
在報表產生器裡,加入每一步驟的輸入與輪出的紀錄功能,XYZ 企業需要這些資料來進行評估審核,喔,對了,
我告訴他們,我們可以在 3 天內完成!!"
],
"metadata": {
"id": "ntDRQK7UeFej"
}
},
{
"cell_type": "markdown",
"source": [
"##Python 的 裝飾器 ( decorator ) 是一個可以讓程式碼達到精簡又漂亮的寫法,用起來不但
輕鬆簡單,又可以提升程式碼的可讀性\n",
"#關於函數:\n",
"- 函數是物件:可以被指派給變數。可以作為參數傳入其他函數,可以做為函數的回傳值\n",
"- 函數可以被定義在另一支函數中\n",
"\n",
"##什麼是裝飾器 decorator?\n",
"- 裝飾器 decorator 是 Python 的一種程式設計模式,\n",
"- 裝飾器本質上是一個 Python 函式或類 ( class ),它可以讓其他函式或類,在不需要做任何
代碼修改的前提下增加額外功能,裝飾器的返回值也是一個函式或類對象,\n",
"- 有了裝飾器,就可以抽離與函式功能本身無關的程式碼,放到裝飾器中並繼續重複使用。\n",
"\n",
"- 在 Python 中,使用「@」當做裝飾器使用的語法糖符號 ( 語法糖指的是將複雜的程式碼包裝起
來的糖衣,也就是簡化寫法 )。\n",
"\n"
],
"metadata": {
"id": "EtKRGDQVcUzB"
}
},
{
"cell_type": "markdown",
"source": [
"###製作第一個裝飾器\n",
"-下方的程式碼,定義了一個裝飾器函式 a 和一個被裝飾的函式 b,當 b 函式執行後,會看見 a
運算後的結果,套用在 b 函式上。\n",
"\n",
"-在 Python 裡,函式 function 可以當成參數傳遞並執行,所以在程式碼中將 b 傳入 a 作為
參數,所以執行 b 時效果等同於執行 a 裡的 func"
],
"metadata": {
"id": "FFO_6EGycuY5"
}
},
{
"cell_type": "code",
"source": [
"def a(func):\n",
" print('makeup...')\n",
" return func\n",
"\n",
"def b():\n",
" print('go!!!!')\n",
"\n",
"c = a(b)\n",
"c()\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CrJKMdDmcthq",
"outputId": "7f9920e8-a836-44e8-d652-ac4873e51b05"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"makeup...\n",
"go!!!!\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"###接著使用語法糖「@」包裝,就能達到簡化的效果。\n"
],
"metadata": {
"id": "8p83XrYhdNIA"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "RjP82_FAcR7V",
"outputId": "d9a807e8-e5ba-4c81-e002-8f0ceaf3b37e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"makeup...\n",
"go!!!!\n"
]
}
],
"source": [
"def a(func):\n",
" print('makeup...')\n",
" return func\n",
"\n",
"# 裝飾器寫在 b 的前面,表示裝飾 b\n",
"@a\n",
"def b():\n",
" print('go!!!!')\n",
"\n",
"b()\n"
]
},
{
"cell_type": "markdown",
"source": [
"###多個裝飾器\n",
"-如果有多個裝飾器,執行的順序將會「由下而上」觸發 ( 函式一層層往上 ),下方的程式碼,會先
裝飾 a3,接著裝飾 a2,最後裝飾 a1。"
],
"metadata": {
"id": "QyUXiWqbdaXv"
}
},
{
"cell_type": "code",
"source": [
"def a1(func):\n",
" print('1')\n",
" return func\n",
"\n",
"def a2(func):\n",
" print('2')\n",
" return func\n",
"\n",
"def a3(func):\n",
" print('3')\n",
" return func\n",
"\n",
"@a1\n",
"@a2\n",
"@a3\n",
"def b():\n",
" print('go!!!!')\n",
"\n",
"b()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vDHFcTUfdhK6",
"outputId": "f7aefb68-40b3-4b42-b850-fdfd173cd498"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3\n",
"2\n",
"1\n",
"go!!!!\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"###單一參數處理\n",
"-如果裝飾器遇到帶有參數的函式,同樣能將參數一併帶入處理,實作的方式如下方程式碼,在裝飾
器函式 a 裡,新增一個函式內的函式 c,並透過函式 c 來獲取被裝飾函式 b 的參數 msg。"
],
"metadata": {
"id": "p1J2boo0dnE0"
}
},
{
"cell_type": "code",
"source": [
"def a(func):\n",
" def c(m): # 新增一個內部函式,待有一個參數\n",
" print('makeup...')\n",
" return func(m) # 回傳 func(m)\n",
" return c\n",
"\n",
"@a\n",
"def b(msg):\n",
" print(msg)\n",
"\n",
"b('go!!!!')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rHI9Hi6Fdtxv",
"outputId": "12d88c95-d79e-4510-b9bc-ebad8f26d08a"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"makeup...\n",
"go!!!!\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"sss=[a*b for b in range(1,10) for a in range(1,10)]\n",
"print(sss)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "afxFJdzsfySL",
"outputId": "569406cb-a95e-4884-f091-8510c063c251"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9,
12, 15, 18, 21, 24, 27, 4, 8, 12, 16, 20, 24, 28, 32, 36, 5, 10, 15, 20, 25, 30,
35, 40, 45, 6, 12, 18, 24, 30, 36, 42, 48, 54, 7, 14, 21, 28, 35, 42, 49, 56, 63,
8, 16, 24, 32, 40, 48, 56, 64, 72, 9, 18, 27, 36, 45, 54, 63, 72, 81]\n"
]
}
]
}
]
}

You might also like