open source软件的最佳功能之一是重复使用其他人的代码。 重复使用代码有助于节省时间、发现新功能和学习其他编程风格。 重复使用代码有两种主要方法:
- 将代码片段直接复制并粘贴到项目中。 如果你不熟悉编码,这是开始重复使用代码的最快方法。
- 将库导入项目。 虽然此方法需要花一些时间进行学习,但最终会更加轻松高效。 这也是软件开发的基础技能。
在本文中,我们将通过一个示例来学习这两者:重用计算数字的阶乘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文件的顶部,将许可证粘贴为注释。
提示
可以使用选择许可证工具了解其他常见许可证所允许的内容。
3.使用和修改代码片段
现在,你已准备好将代码片段粘贴到项目中。 虽然有时可以直接按原样使用代码片段,但经常需要根据自己的具体用例对其进行修改****。 现在我们来练习一下吧!
假设我们想要快速计算 5、7、9 和 10 的阶乘。 我们可以把计算器放在一个以参数形式接收数字的函数中,而不是为每个数字复制粘贴整个程序****。
使用 副驾驶聊天 来建议和解释实现。 将当前代码粘贴到聊天窗口中,后接以下提示:
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.查找库
一旦你知道想要添加到项目的功能,就可以查找包含相关代码的库。 副驾驶聊天 是搜索库的一种简单方法,因为可以使用自然语言来准确描述要查找的内容。
查找阶乘是一个相当常见的函数,而且很有可能有人已在现有库中添加了这个函数。 打开 副驾驶聊天,然后询问:
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 。
打开 副驾驶聊天,然后询问:
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 alerts
启用后,当 Dependabot alerts 检测到依赖项中的安全问题时,Dependabot 会自动生成,从而帮助你快速修复漏洞。 Dependabot可在所有开放源代码**** 存储库上GitHub获取。
立即为存储库启用 Dependabot alerts 。 单击项目 Security and quality存储库的GitHub 选项卡。 在Dependabot alerts旁边,单击启用 Dependabot alerts。 可以从边栏的选项卡访问Dependabot alertsDependabot。
3.从库实现代码
现在,你已准备好将库导入项目中,然后在代码中使用其内容。 你可以阅读该库的文档,学习如何自己完成这个任务,也可以请 Copilot 为你建议并解释一个实现方案。
打开 副驾驶聊天,然后询问:
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}")
将项目中的现有代码替换为上述实现后,你已成功在示例项目中使用库中的代码!
共享你的工作
本教程介绍了如何在自己的工作中安全地重复使用其他人的代码。 若要展示你的成功,请在我们的社区讨论中分享你是如何重新利用代码并基于示例项目进行构建的。