Skip to content

Commit

Permalink
🎉auto update by Gmeek action
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronChen5651 committed Dec 17, 2024
1 parent b8f860c commit b42cc4c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AARON's BLOG :link: https://AaronChen5651.github.io/aaron.github.io
### :page_facing_up: [3](https://AaronChen5651.github.io/aaron.github.io/tag.html)
### :speech_balloon: 0
### :hibiscus: 4259
### :alarm_clock: 2024-12-17 12:09:30
### :hibiscus: 5433
### :alarm_clock: 2024-12-17 14:36:43
### Powered by :heart: [Gmeek](https://github.com/Meekdai/Gmeek)
56 changes: 56 additions & 0 deletions backup/UC Berkeley CS61A Sping 2021 Lecture 3 Control.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,59 @@ b = square(4) # 创建新的局部环境,x = 4
4. **提高可复用性**,使函数能够独立运行。

局部环境使函数调用成为一个**封闭的计算单元**,有效管理变量作用域,保证程序的正确性和可维护性。

---
---
### **return f 和 return f(x) 的区别**

return f 返回的是 函数对象 f。
return f(x) 返回的是 函数 f 执行后的结果,即 f(x) 的计算值。

报错举例:
你遇到的错误是:

```python
NameError: name 'x' is not defined
```

这是因为在函数 `incr(n)` 中,你尝试直接调用 `f(x)`,但是在此时 `x` 并没有定义。这导致 Python 无法找到 `x` 的值,产生了 `NameError` 错误。

### 具体原因:

```python
def incr(n):
def f(x):
return n + x
return f(x) # 错误:x 在这里没有定义
```

在这个代码中:
- `f(x)` 是一个内嵌函数,它需要一个 `x` 作为参数。
- 但是你在 `incr(n)` 函数内部直接调用了 `f(x)`,并且没有给 `x` 传递任何值,这会导致 Python 无法理解 `x` 是什么。

### 正确的做法:

如果你的目的是返回 `f` 函数本身(即将 `f` 作为闭包返回),而不是立即执行它,你应该改为 `return f`,而不是 `return f(x)`。然后,稍后你可以使用参数来调用 `f`

### 修改后的代码:

```python
def incr(n):
def f(x):
return n + x
return f # 返回函数 f 本身,而不是调用 f(x)

result_function = incr(5) # 返回 f 函数
result = result_function(6) # 调用 f(6)
print(result) # 输出 5 + 6 = 11
```

### 解释:
1. `incr(5)` 返回的是 `f` 函数,而不是 `f(x)` 的执行结果。
2. 返回的 `f` 函数被赋值给 `result_function`,然后我们调用 `result_function(6)`,即实际调用的是 `f(6)`
3. `f(6)` 会计算 `n + x`,其中 `n` 是从 `incr(5)` 中传递来的 `5`,所以 `f(6)` 返回的是 `5 + 6 = 11`,最终打印结果是 `11`

### 关键修改:
- 使用 `return f` 返回函数对象,而不是直接调用 `f(x)`

这样就能正确地实现闭包的功能。
2 changes: 1 addition & 1 deletion blogBase.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"singlePage": [], "startSite": "12/10/2024", "filingNum": "", "onePageListNum": 15, "commentLabelColor": "#006b75", "yearColorList": ["#bc4c00", "#0969da", "#1f883d", "#A333D0"], "i18n": "CN", "themeMode": "manual", "dayTheme": "light", "nightTheme": "dark_colorblind", "urlMode": "pinyin", "script": "", "style": "", "head": "", "indexScript": "", "indexStyle": "", "bottomText": "", "showPostSource": 1, "iconList": {}, "UTC": 8, "rssSplit": "sentence", "exlink": {}, "needComment": 1, "allHead": "", "title": "AARON's BLOG", "subTitle": "----------------------", "avatarUrl": "https://github.githubassets.com/favicons/favicon.svg", "GMEEK_VERSION": "last", "postListJson": {"P1": {"htmlDir": "docs/post/test.html", "labels": ["test"], "postTitle": "test", "postUrl": "post/test.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/1", "commentNum": 0, "wordCount": 4, "description": "test\u3002", "top": 0, "createdAt": 1733794020, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-10", "dateLabelColor": "#bc4c00"}, "P3": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 2 Functions.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 2 Functions", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20%20Lecture%202%20Functions.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/3", "commentNum": 0, "wordCount": 1396, "description": "lecture2\u4e3b\u8981\u8bb2\u89e3\u51fd\u6570\u8c03\u7528\u8fc7\u7a0b\uff0c\u7eaf\u51fd\u6570\u3001\u975e\u7eaf\u51fd\u6570\uff0c\u4ece\u73af\u5883\u89d2\u5ea6\u89e3\u91ca\u51fd\u6570\u8c03\u7528\u7b49\u3002", "top": 0, "createdAt": 1734274908, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-15", "dateLabelColor": "#bc4c00"}, "P4": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 3 Control.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 3 Control", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20Lecture%203%20Control.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/4", "commentNum": 0, "wordCount": 2859, "description": "### **\u73af\u5883\u94fe\u6761\uff08Environment Chain\uff09**\r\nfor example\uff1a\r\n![\u5c4f\u5e55\u622a\u56fe 2024-12-17 114451](https://github.com/user-attachments/assets/f19c7928-68f6-4753-9118-5ef3482f9ae1)\r\n\r\n---\r\n---\r\n\r\n\r\n### **\u73af\u5883\u4e2d\u7684\u7ed1\u5b9a\u548c\u6c42\u503c\uff08Binding and Evaluation\uff09**\r\n\r\n\r\n\r\n### 1. **\u53d8\u91cf\u8d4b\u503c\uff08Assigning\uff09** \r\n- **\u8d4b\u503c**\uff08Assigning\uff09\u5c06\u4e00\u4e2a\u6570\u503c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002", "top": 0, "createdAt": 1734407281, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-17", "dateLabelColor": "#bc4c00"}}, "singeListJson": {}, "labelColorDict": {"bug": "#d73a4a", "documentation": "#0075ca", "duplicate": "#cfd3d7", "enhancement": "#a2eeef", "good first issue": "#7057ff", "help wanted": "#008672", "invalid": "#e4e669", "lecture note": "#d4c5f9", "question": "#d876e3", "test": "#d93f0b", "wontfix": "#ffffff"}, "displayTitle": "AARON's BLOG", "faviconUrl": "https://github.githubassets.com/favicons/favicon.svg", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "primerCSS": "<link href='https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/Primer/21.0.7/primer.css' rel='stylesheet' />", "homeUrl": "https://AaronChen5651.github.io/aaron.github.io", "prevUrl": "disabled", "nextUrl": "disabled"}
{"singlePage": [], "startSite": "12/10/2024", "filingNum": "", "onePageListNum": 15, "commentLabelColor": "#006b75", "yearColorList": ["#bc4c00", "#0969da", "#1f883d", "#A333D0"], "i18n": "CN", "themeMode": "manual", "dayTheme": "light", "nightTheme": "dark_colorblind", "urlMode": "pinyin", "script": "", "style": "", "head": "", "indexScript": "", "indexStyle": "", "bottomText": "", "showPostSource": 1, "iconList": {}, "UTC": 8, "rssSplit": "sentence", "exlink": {}, "needComment": 1, "allHead": "", "title": "AARON's BLOG", "subTitle": "----------------------", "avatarUrl": "https://github.githubassets.com/favicons/favicon.svg", "GMEEK_VERSION": "last", "postListJson": {"P1": {"htmlDir": "docs/post/test.html", "labels": ["test"], "postTitle": "test", "postUrl": "post/test.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/1", "commentNum": 0, "wordCount": 4, "description": "test\u3002", "top": 0, "createdAt": 1733794020, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-10", "dateLabelColor": "#bc4c00"}, "P3": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 2 Functions.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 2 Functions", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20%20Lecture%202%20Functions.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/3", "commentNum": 0, "wordCount": 1396, "description": "lecture2\u4e3b\u8981\u8bb2\u89e3\u51fd\u6570\u8c03\u7528\u8fc7\u7a0b\uff0c\u7eaf\u51fd\u6570\u3001\u975e\u7eaf\u51fd\u6570\uff0c\u4ece\u73af\u5883\u89d2\u5ea6\u89e3\u91ca\u51fd\u6570\u8c03\u7528\u7b49\u3002", "top": 0, "createdAt": 1734274908, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-15", "dateLabelColor": "#bc4c00"}, "P4": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 3 Control.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 3 Control", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20Lecture%203%20Control.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/4", "commentNum": 0, "wordCount": 4033, "description": "### **\u73af\u5883\u94fe\u6761\uff08Environment Chain\uff09**\r\nfor example\uff1a\r\n![\u5c4f\u5e55\u622a\u56fe 2024-12-17 114451](https://github.com/user-attachments/assets/f19c7928-68f6-4753-9118-5ef3482f9ae1)\r\n\r\n---\r\n---\r\n\r\n\r\n### **\u73af\u5883\u4e2d\u7684\u7ed1\u5b9a\u548c\u6c42\u503c\uff08Binding and Evaluation\uff09**\r\n\r\n\r\n\r\n### 1. **\u53d8\u91cf\u8d4b\u503c\uff08Assigning\uff09** \r\n- **\u8d4b\u503c**\uff08Assigning\uff09\u5c06\u4e00\u4e2a\u6570\u503c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002", "top": 0, "createdAt": 1734407281, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-17", "dateLabelColor": "#bc4c00"}}, "singeListJson": {}, "labelColorDict": {"bug": "#d73a4a", "documentation": "#0075ca", "duplicate": "#cfd3d7", "enhancement": "#a2eeef", "good first issue": "#7057ff", "help wanted": "#008672", "invalid": "#e4e669", "lecture note": "#d4c5f9", "question": "#d876e3", "test": "#d93f0b", "wontfix": "#ffffff"}, "displayTitle": "AARON's BLOG", "faviconUrl": "https://github.githubassets.com/favicons/favicon.svg", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "primerCSS": "<link href='https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/Primer/21.0.7/primer.css' rel='stylesheet' />", "homeUrl": "https://AaronChen5651.github.io/aaron.github.io", "prevUrl": "disabled", "nextUrl": "disabled"}
43 changes: 42 additions & 1 deletion docs/post/UC Berkeley CS61A Sping 2021 Lecture 3 Control.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,48 @@ <h3>5. <strong>总结:为什么会创建新的局部环境?</strong></h3>
<li><strong>支持递归</strong>,为每一层调用保存独立的状态。</li>
<li><strong>提高可复用性</strong>,使函数能够独立运行。</li>
</ol>
<p>局部环境使函数调用成为一个<strong>封闭的计算单元</strong>,有效管理变量作用域,保证程序的正确性和可维护性。</p></div>
<p>局部环境使函数调用成为一个<strong>封闭的计算单元</strong>,有效管理变量作用域,保证程序的正确性和可维护性。</p>
<hr>
<hr>
<h3><strong>return f 和 return f(x) 的区别</strong></h3>
<p>return f 返回的是 函数对象 f。<br>
return f(x) 返回的是 函数 f 执行后的结果,即 f(x) 的计算值。</p>
<p>报错举例:<br>
你遇到的错误是:</p>
<div class="highlight highlight-source-python"><pre class="notranslate"><span class="pl-v">NameError</span>: <span class="pl-s1">name</span> <span class="pl-s">'x'</span> <span class="pl-c1"><span class="pl-c1">is</span> <span class="pl-c1">not</span></span> <span class="pl-s1">defined</span></pre></div>
<p>这是因为在函数 <code class="notranslate">incr(n)</code> 中,你尝试直接调用 <code class="notranslate">f(x)</code>,但是在此时 <code class="notranslate">x</code> 并没有定义。这导致 Python 无法找到 <code class="notranslate">x</code> 的值,产生了 <code class="notranslate">NameError</code> 错误。</p>
<h3>具体原因:</h3>
<div class="highlight highlight-source-python"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">incr</span>(<span class="pl-s1">n</span>):
<span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>):
<span class="pl-k">return</span> <span class="pl-s1">n</span> <span class="pl-c1">+</span> <span class="pl-s1">x</span>
<span class="pl-k">return</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>) <span class="pl-c"># 错误:x 在这里没有定义</span></pre></div>
<p>在这个代码中:</p>
<ul>
<li><code class="notranslate">f(x)</code> 是一个内嵌函数,它需要一个 <code class="notranslate">x</code> 作为参数。</li>
<li>但是你在 <code class="notranslate">incr(n)</code> 函数内部直接调用了 <code class="notranslate">f(x)</code>,并且没有给 <code class="notranslate">x</code> 传递任何值,这会导致 Python 无法理解 <code class="notranslate">x</code> 是什么。</li>
</ul>
<h3>正确的做法:</h3>
<p>如果你的目的是返回 <code class="notranslate">f</code> 函数本身(即将 <code class="notranslate">f</code> 作为闭包返回),而不是立即执行它,你应该改为 <code class="notranslate">return f</code>,而不是 <code class="notranslate">return f(x)</code>。然后,稍后你可以使用参数来调用 <code class="notranslate">f</code></p>
<h3>修改后的代码:</h3>
<div class="highlight highlight-source-python"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">incr</span>(<span class="pl-s1">n</span>):
<span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>):
<span class="pl-k">return</span> <span class="pl-s1">n</span> <span class="pl-c1">+</span> <span class="pl-s1">x</span>
<span class="pl-k">return</span> <span class="pl-s1">f</span> <span class="pl-c"># 返回函数 f 本身,而不是调用 f(x)</span>

<span class="pl-s1">result_function</span> <span class="pl-c1">=</span> <span class="pl-en">incr</span>(<span class="pl-c1">5</span>) <span class="pl-c"># 返回 f 函数</span>
<span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-en">result_function</span>(<span class="pl-c1">6</span>) <span class="pl-c"># 调用 f(6)</span>
<span class="pl-en">print</span>(<span class="pl-s1">result</span>) <span class="pl-c"># 输出 5 + 6 = 11</span></pre></div>
<h3>解释:</h3>
<ol>
<li><code class="notranslate">incr(5)</code> 返回的是 <code class="notranslate">f</code> 函数,而不是 <code class="notranslate">f(x)</code> 的执行结果。</li>
<li>返回的 <code class="notranslate">f</code> 函数被赋值给 <code class="notranslate">result_function</code>,然后我们调用 <code class="notranslate">result_function(6)</code>,即实际调用的是 <code class="notranslate">f(6)</code></li>
<li><code class="notranslate">f(6)</code> 会计算 <code class="notranslate">n + x</code>,其中 <code class="notranslate">n</code> 是从 <code class="notranslate">incr(5)</code> 中传递来的 <code class="notranslate">5</code>,所以 <code class="notranslate">f(6)</code> 返回的是 <code class="notranslate">5 + 6 = 11</code>,最终打印结果是 <code class="notranslate">11</code></li>
</ol>
<h3>关键修改:</h3>
<ul>
<li>使用 <code class="notranslate">return f</code> 返回函数对象,而不是直接调用 <code class="notranslate">f(x)</code></li>
</ul>
<p>这样就能正确地实现闭包的功能。</p></div>
<div style="font-size:small;margin-top:8px;float:right;"></div>

<button class="btn btn-block" type="button" onclick="openComments()" id="cmButton">评论</button>
Expand Down

0 comments on commit b42cc4c

Please sign in to comment.