开源软件的最大优势之一是可以复用他人的代码。重新利用代码可以帮助你节省时间、发现新功能并学习其他编程风格。复用代码主要有两种方式。
- 复制并粘贴代码片段直接到你的项目中。 如果你是编码新手,这是开始复用代码的最快方式。
- 将库导入到你的项目中。 虽然这种方法需要一些学习时间,但最终更简便高效。这也是软件开发的基础技能。
在本文中,我们将通过示例学习两者:复用计算数字阶乘的 Python 代码。
在项目中使用他人的代码片段
当你刚开始学习编码时,可能会通过复制粘贴他人的代码片段来复用代码。这是节省时间的好方法,但在复制其他开发者的代码之前,你应该始终遵循几个关键步骤。
1. 查找并理解代码片段
首先,你需要找到并理解想要复用的代码片段。本文示例我们将使用 new2code/python-factorial 仓库。
首先,打开 factorial_finder.py,它使用循环实现计算器。
# Initialize the factorial result to 1
factorial = 1
# Initialize the input number to 6
number = 6
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
print(f"The factorial of {number} is {factorial}")
然后,在文件顶部的菜单栏中,点击以开始与 Copilot 对话。

在聊天窗口中,向 Copilot 提问
Explain this program.
Explain this program.
2. 理解项目许可证
在复用你找到的代码之前,需要先了解它的许可证。许可证决定了你在项目中如何使用代码,包括复制、修改和分发的权限。
要确定 new2code/python-factorial 的许可证,请在仓库主页的 “About” 部分查找。你会看到该仓库采用 MIT 许可证。要阅读许可证,请点击 MIT 许可证.

我们想复制整个 factorial_finder.py 文件,所以 MIT 许可证要求我们在自己的项目中包含一份许可证副本。请在你的 Python 文件顶部以注释形式粘贴该许可证。
提示
你可以使用 Choose a license 工具了解其他常见许可证的允许范围。
3. 使用和修改代码片段
现在,你可以把代码片段粘贴到项目中。虽然有时可以直接使用代码片段,但通常你会希望修改它们以符合特定需求。现在来练习一下吧!
假设我们想快速计算 5、7、9、10 的阶乘。与其为每个数字复制整个程序,我们可以把计算器封装进一个函数,该函数接受一个数字作为参数。
使用 Copilot Chat 来建议并解释实现方式。将我们当前的代码粘贴到聊天窗口中,然后输入以下提示
Wrap the Python code above in a function.
Wrap the Python code above in a function.
Copilot 将生成类似下面的代码
def calculate_factorial(number):
# Initialize the factorial result to 1
factorial = 1
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
return factorial
def calculate_factorial(number):
# Initialize the factorial result to 1
factorial = 1
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
return factorial
有了新函数后,我们只需在项目中添加以下代码并运行 Python 程序,即可轻松得到这些数字的阶乘。
print(calculate_factorial(5)) print(calculate_factorial(7)) print(calculate_factorial(9)) print(calculate_factorial(10))
print(calculate_factorial(5))
print(calculate_factorial(7))
print(calculate_factorial(9))
print(calculate_factorial(10))
恭喜!你已成功查找、理解并修改了示例代码片段。
在项目中使用库中的代码
现在,让我们学习如何使用库,这对开发者来说是标准做法。库本质上是其他开发者编写的、用于执行特定任务的代码集合。你可以将库导入到项目中,以使用预写好的代码,从而节省时间和精力。
在本节中,我们将继续使用前一节的 Python 阶乘计算器示例。供参考,这里是我们当前的代码。
def calculate_factorial(number):
# Initialize the factorial result to 1
factorial = 1
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
return factorial
print(calculate_factorial(5))
print(calculate_factorial(7))
print(calculate_factorial(9))
print(calculate_factorial(10))
def calculate_factorial(number):
# Initialize the factorial result to 1
factorial = 1
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
return factorial
print(calculate_factorial(5))
print(calculate_factorial(7))
print(calculate_factorial(9))
print(calculate_factorial(10))
1. 查找库
确定想在项目中添加的功能后,你可以搜索包含相关代码的库。Copilot Chat 是搜索库的便捷方式,因为你可以使用自然语言准确描述需求。
阶乘函数是相当常见的功能,很可能已经被纳入某个现有库。打开 Copilot Chat,然后问
Is there a Python library with a function for calculating a factorial?
Is there a Python library with a function for calculating a factorial?
Copilot 会告诉我们,标准 Python 库的 math 模块中已包含阶乘函数。
2. 在项目中优先考虑安全
当你向项目添加库或模块时,会产生所谓的依赖。依赖是项目正常运行所依赖的预写代码包。如果这些依赖没有被仔细编写或维护,可能会给你的工作带来安全漏洞。
幸运的是,你可以采取一些措施来最佳地保护你的项目。现在让我们一起练习这些步骤。
使用流行的库
流行的库更可能是安全的,因为它们由众多开发者积极维护并广泛使用。判断流行程度的一个好指标是仓库的星标数量。如果找不到某个依赖的 GitHub 仓库,你可以请求 Copilot 帮助。
打开 Copilot Chat,然后问
Find the GitHub repository containing the code for the math module in Python.
Find the GitHub repository containing the code for the math module in Python.
Copilot 会告诉你,math 模块在 python/cpython 中定义,该仓库拥有超过 64,000 颗星。
为你的项目启用 Dependabot 警报
启用后,当 Dependabot 检测到依赖中的安全问题时,会自动生成警报,帮助你快速修复漏洞。Dependabot 对所有开源 GitHub 仓库免费提供。
立即为你的仓库打开 Dependabot 警报。点击项目 GitHub 仓库的 安全性和质量 选项卡。 在 Dependabot 警报旁边,点击 启用 Dependabot 警报。 你可以在侧边栏的 Dependabot 选项卡中访问 Dependabot 警报。
3. 实现来自库的代码
现在,你可以将库导入到项目中,并在代码里使用其内容。你可以阅读库的文档自行学习实现方式,或者请求 Copilot 为你建议并解释实现方案。
打开 Copilot Chat,然后问
How do I use the factorial function of the math module in my Python project?
How do I use the factorial function of the math module in my Python project?
Copilot 将会建议下面代码的一个实现版本
import math
# Calculate the factorial of a number
number = 5
result = math.factorial(number)
print(f"The factorial of {number} is {result}")
import math
# Calculate the factorial of a number
number = 5
result = math.factorial(number)
print(f"The factorial of {number} is {result}")
在项目中用上述实现替换掉原有代码后,你已成功在示例项目中使用了库代码!
分享你的工作
通过本教程,你已经学会了如何安全地在自己的工作中复用他人的代码。为此,请在我们的 社区讨论中分享你是如何重新利用代码并在示例项目上进行二次构建的。