作為可重用區塊的Python函數
紫式晦澀每日一篇文章第34天
前言
-
今天是2022年第32天, 全年第5週, 一月的第5個週二. 今天思考「可重用性(Reusability)」. 在Python裡面「可重用性」的最基礎, 就是「寫好函數」的技能. 今天利用文章來組織這項軟技能.
-
今天的素材主要來自文章:
- 2018: Get Programming Learn to code with Python 的第五單元(P.181-232). 此單元完整講解了如何執行模組化與抽象化,
收集「三」的表達.
心法: 模組化大事化小, 抽象化以簡馭繁, 形式參數開發者, 實際參數使用者
三課內容:
- 20 建築能持續的程序
- 21 利用函數達到模組化與抽象
- 22 函數的進階操作
三課小節: 共11小節:
- 20.1 Breaking a big task into smaller tasks:
- 20.2 Introducing black boxes of code in programming
- 20.3 Subtasks exist in their own environments
- 21.1 Writing a function
- 21.2 Using functions
- 21.3 Documenting your functions
- 22.1 Thinking about functions with two hats
- 22.2 Function scope
- 22.3 Nesting functions
- 22.4 Passing functions as parameters
- 22.5 Returning a function
三課小小節: 共11小節:
- 20.1 Breaking a big task into smaller tasks:
-
- 20.1.1.任務相關/獨立性(Task dependence/independence).
-
- 20.1.2.黑盒子(black box)-模組(Module).
- 20.2 Introducing black boxes of code in programming
-
- 20.2.1. Using code modules: 模組化(Modularity)
-
- 20.2.2. Abstracting code: 字符串(docstring)
-
- 20.2.3. Reusing code: 代碼包裝(code wrapper)
- 20.3 Subtasks exist in their own environments: 切任務成子任務(dividing a larger task into subtasks), 細節抽象化(abstraction of details).
- 21.1 Writing a function
-
- 21.1.1. 輸入 Function basics: what the function takes in: 參數有兩個英文, parameters, arguments. 在設計函數的層面, 是「形式參數(formal parameters, formal arguments)」
-
- 21.1.2. 處理 Function basics: what the function does
-
- 21.1.3 輸出 Function basics: what the function returns
- 21.2 Using functions: 真實參數(Actual parameters).
- 21.2.1 Returning more than one value: 雖然函數只會回傳「一個物件(a object)」, 但我們可以把需要的東西都包裝成物件.
-
- 21.2.2 Functions without a return statement: 沒寫return的話, Python automatically returns the value None in the function.
- 21.3 Documenting your functions:函數指定(function specifications)或字符串(docstrings)來實踐抽象化.
- 22.1 Thinking about functions with two hats
-
- 22.1.1 Writer hat: 輸入為「形式參數(Formal parameters)」
-
- 22.1.2 User hat: 輸入為「實際參數(Actual parameters)」
- 22.2 Function scope
-
- 22.2.1 Simple scoping example 作用域影響函數回傳
-
- 22.2.2 Scoping rules 作用域規則
- 22.3 Nesting functions
- 22.4 Passing functions as parameters
- 22.5 Returning a function
Lesson 20: 建築能持續的程序
Lesson 20:建築能持續的程序:
- 如何: 大任務成小模組(how a bigger task is divided into modules)
- 為何: 隱藏複雜任務的細節( why you should hide away details of complicated tasks)
- 是什麼: 任務之間「獨立」與「相依」的意涵 ( what it means for tasks to be dependent on or independent ofother tasks)
Lesson 21: 利用函數達到模組化與抽象
Lesson 21 利用函數達到模組化與抽象:
- 寫使用「函數(Functions)」的代碼
- 寫有「參數(Parameters)」的函數
- 寫會「回傳特定職(return a specified value)」的函數
- 理解「變數值(Variable)」是如何在不同函數環境下變化
模組化大事化小;抽象化以簡馭繁:
- 模組化: 把問題拆小, 一個個解決. Modularity is having smaller (more or less independent) problems to tackle, one by one.
- 抽象化: 以模組為單位思考,不擔心模組內細節. Abstraction is being able to think about the modules themselves at a higher level, without worrying about the details of implementing each.
模組化大事化小;抽象化以簡馭繁
Lesson 22: 函數的進階操作
Lesson 22 函數的進階操作:
- 將「函數」作為「另一函數的參數」(Pass functions (as an object) as a parameter to another function)
- 從「函數」回傳「作為物件的函數」(Return a function (as an object) from another function)
- 利用特定規則, 理解變數所在的「作用域(Scope)」(Understand which variables belong to which scope based on certain rules)
****: ****:
技法
字符串(Docstring):
- A docstring contains the following information:
-
- All inputs to the module—Represented by variables and their types.
-
- What the module is supposed to do—Its function.
-
- What output the module gives you—This might be an object (variable) or it might be something that the module prints.
****: ****: ****: ****: ****:
用法
****: ****: ****: ****: ****: ****:
文法
相關任務(Dependent Task):
- 一個任務要依賴於另一個任務結束才能開始, 就是相關任務.
- 獨立任務: 兩個任務獨立, 他們就可以同時做.
DEFINITION A task depends on another one if it can’t start before the other one completes. Two tasks are independent if they can be performed at the same time.
任務抽象(Abstraction of a task):
- 用最少資訊就可以理解任務內容; 隱藏不必要的細節.
- Definition Abstraction of a task is a way to simplify the task such that you understand it by using the least amount of information; you hide all unnecessary details.
可重用子任務(Reusable subtasks):
- 其步驟, 可以用不同的輸入, 來得到不同的輸出
- DEFINITION Reusable subtasks are tasks whose steps can be reused with different inputs to produce different output.
黑盒子(Black box):
- 可視化做某件任務的系統.
- DEFINITION A black box is a way to visualize a system that does a certain task. A black box on top of the system reminds you that you don’t get to [or need to] see inside the box in order to understand what the system does.
模塊(Module): IPO-輸入, 任務, 輸出:
- 代碼模塊: 達成特定任務的代碼
- 模塊: 輸入-任務-輸出
- A code module is a piece of code that achieves a certain task. A module is associated with input, a task, and output.
函數(Function):簡單任務的代碼模組:
- 函數輸入; 函數處理; 函數輸出
- In many programing languages, a function is used to stand for a module of code that achieves a simple task. When you’re writing a function, you have to think about three things:
-
- What input the function takes in
-
- What operations/calculations the function does
-
- What the function returns
作用域(Scope):
- 同name的變數, 要靠作用域來區分.
****: ****:
後記
Ver0.1 (2/1): 今天對文章中各種圖, 文章結構想強調的點, 進行了80分鐘的思考.本文章的結構非常好, 從「連環圖」的角度講了很棒的科學研究故事. 多欣賞好文章, 來讓自己的文章的readability上升. 又追加30分鐘, 把一些專有名詞與配圖補上. 要把函數寫好, 心中要常有「可複用」的原則, 才可以抽象鍛鍊出好的「函數」. 作為函數的文本, 要如何在不同的paper中重複被使用呢?
對於證明類的文本, 如何做圖與可視化呢? 收集好的文獻?
2022.02.01. 紫蕊 於 西拉法葉, 印第安納, 美國.
評論