issues
search
cssmagic
/
Learn-AI-Assisted-Python-Programming
📖 《AI 辅助编程 Python 实战》这本书的大本营。
142
stars
15
forks
source link
[译] [307] 本章小结
#28
Open
cssmagic
opened
9 months ago
cssmagic
commented
9 months ago
Summary
本章小结
Problem decomposition involves breaking a large problem into smaller tasks.
We use functions to perform problem decomposition in our programs.
Each function must solve one small, well-defined task.
Functions reduce duplication, make it easier to test our code, and reduce the likelihood of bugs.
Unit testing involves checking that the function does what we expect on a variety of different inputs.
A function header or signature is the first line of code of the function.
Parameters are used to provide information to functions.
The function header indicates the name of the function and names of its parameters.
We use return to output a value from a function.
A docstring uses the names of each function parameter to describe the purpose of the function.
To ask Copilot to write a function, we provide it the function header and docstring.
We get a function to do its work by calling it with values (also called arguments) for its parameters.
A variable is a name that refers to a value.
A helper function is a small function written to make it easier to write a bigger function.
A leaf function doesn’t call any other function to do its job.
To test whether a function is correct, we call it with different types of inputs.
Every Python value has a type, such as a number, text (string), true/false value (bool), or collection of values (list or dictionary).
Prompt engineering involves modifying our prompt for Copilot to influence the code that we get back.
We need to ensure that we import any module (such as string) our code is using.
“问题分解”是指将一个大问题拆解为多个小任务。
我们利用函数来实现程序中的问题分解。
每个函数都应解决一个小而具体的任务。
函数可以减少代码重复,让测试代码变得更容易,并且减少了出现 bug 的可能性。
“单元测试”是我们通过各种不同的输入来验证函数的行为是否符合我们的预期。
“函数头”或“函数签名”是该函数的首行代码。
“参数”用于向函数传递信息。
函数头显示了函数的名称和它的参数名称。
我们使用
return
来让函数返回一个值。
文档字符串会用到函数的各个参数名来描述函数的用途。
要让 Copilot 编写函数,我们需要提供函数头和相关的文档字符串。
我们在调用函数时需要为其参数传入具体的值(也称为“实参”),从而让它发挥出实际的作用。
“变量”是一个名字,指向一个值。
“辅助函数”是一种小型函数,我们写它是为了简化更大函数的编写过程。
“叶子函数”在完成自身任务时,不会调用其他任何函数。
为了测试函数是否正确,我们需要用不同类型的输入去调用它。
在 Python 中,每个值都有类型,比如数字、文本(字符串)、真/假(布尔值)、或多个值的集合(列表或字典)。
“提示工程”指的是我们修改向 Copilot 给出的提示词,从而影响它给出的代码。
我们需要确保代码用到的任何模块(例如
string
)都已经导入了。
Summary
本章小结
return
来让函数返回一个值。string
)都已经导入了。