# 在你的项目中重复使用其他人的代码

通过将现有代码集成到项目中来提高编码效率和知识量。

open source软件的最佳功能之一是重复使用其他人的代码。 重复使用代码有助于节省时间、发现新功能和学习其他编程风格。 重复使用代码有两种主要方法：

* **将代码片段直接复制并粘贴到项目中。** 如果你不熟悉编码，这是开始重复使用代码的最快方法。
* **将库导入项目。** 虽然此方法需要花一些时间进行学习，但最终会更加轻松高效。 这也是软件开发的基础技能。

在本文中，我们将通过一个示例来学习这两者：重用计算数字的阶乘Python代码。

## 在你的项目中使用其他人的代码片段

刚开始学习编码时，你可能会通过将其他人的代码片段复制并粘贴到项目中来进行重复使用。 这是一种节省时间的好方法，但在复制其他开发人员的代码前，应始终执行几个关键步骤。

### 1.查找和理解代码片段

首先，你需要查找并了解要重复使用的代码片段。 对于此示例，我们将选择 [`new2code/python-factorial`](https://github.com/new2code/python-factorial) 存储库。

首先，打开\*\*\*\*[`factorial_finder.py`](https://github.com/new2code/python-factorial/blob/main/factorial_finder.py)，通过循环实现计算器：

```python
# 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}")
```

然后，在文件顶部的菜单栏中，单击 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-copilot" aria-label="Ask Copilot about this file" role="img"><path d="M7.998 15.035c-4.562 0-7.873-2.914-7.998-3.749V9.338c.085-.628.677-1.686 1.588-2.065.013-.07.024-.143.036-.218.029-.183.06-.384.126-.612-.201-.508-.254-1.084-.254-1.656 0-.87.128-1.769.693-2.484.579-.733 1.494-1.124 2.724-1.261 1.206-.134 2.262.034 2.944.765.05.053.096.108.139.165.044-.057.094-.112.143-.165.682-.731 1.738-.899 2.944-.765 1.23.137 2.145.528 2.724 1.261.566.715.693 1.614.693 2.484 0 .572-.053 1.148-.254 1.656.066.228.098.429.126.612.012.076.024.148.037.218.924.385 1.522 1.471 1.591 2.095v1.872c0 .766-3.351 3.795-8.002 3.795Zm0-1.485c2.28 0 4.584-1.11 5.002-1.433V7.862l-.023-.116c-.49.21-1.075.291-1.727.291-1.146 0-2.059-.327-2.71-.991A3.222 3.222 0 0 1 8 6.303a3.24 3.24 0 0 1-.544.743c-.65.664-1.563.991-2.71.991-.652 0-1.236-.081-1.727-.291l-.023.116v4.255c.419.323 2.722 1.433 5.002 1.433ZM6.762 2.83c-.193-.206-.637-.413-1.682-.297-1.019.113-1.479.404-1.713.7-.247.312-.369.789-.369 1.554 0 .793.129 1.171.308 1.371.162.181.519.379 1.442.379.853 0 1.339-.235 1.638-.54.315-.322.527-.827.617-1.553.117-.935-.037-1.395-.241-1.614Zm4.155-.297c-1.044-.116-1.488.091-1.681.297-.204.219-.359.679-.242 1.614.091.726.303 1.231.618 1.553.299.305.784.54 1.638.54.922 0 1.28-.198 1.442-.379.179-.2.308-.578.308-1.371 0-.765-.123-1.242-.37-1.554-.233-.296-.693-.587-1.713-.7Z"></path><path d="M6.25 9.037a.75.75 0 0 1 .75.75v1.501a.75.75 0 0 1-1.5 0V9.787a.75.75 0 0 1 .75-.75Zm4.25.75v1.501a.75.75 0 0 1-1.5 0V9.787a.75.75 0 0 1 1.5 0Z"></path></svg> 以开始对话 Copilot。

![屏幕截图显示文件视图顶部以深橙色框出的 Copilot 按钮。](/assets/images/help/copilot/factorial-finder-copilot-button.png)

在聊天窗口中，询问 Copilot：

```text copy
Explain this program.
```

### 2.了解项目许可

在可以重复使用找到的代码之前，需要了解其许可。 许可证决定了在项目中使用代码的方式，包括是否能够复制、修改和分发该代码。

若要确定 [new2code/python-factorial](https://github.com/new2code/python-factorial) 的许可证，请找到存储库主页上的“About”部分。 在此，你将看到该存储库已根据 MIT 许可证获得许可。 若要读取许可证，请单击 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-law" aria-label="law" role="img"><path d="M8.75.75V2h.985c.304 0 .603.08.867.231l1.29.736c.038.022.08.033.124.033h2.234a.75.75 0 0 1 0 1.5h-.427l2.111 4.692a.75.75 0 0 1-.154.838l-.53-.53.529.531-.001.002-.002.002-.006.006-.006.005-.01.01-.045.04c-.21.176-.441.327-.686.45C14.556 10.78 13.88 11 13 11a4.498 4.498 0 0 1-2.023-.454 3.544 3.544 0 0 1-.686-.45l-.045-.04-.016-.015-.006-.006-.004-.004v-.001a.75.75 0 0 1-.154-.838L12.178 4.5h-.162c-.305 0-.604-.079-.868-.231l-1.29-.736a.245.245 0 0 0-.124-.033H8.75V13h2.5a.75.75 0 0 1 0 1.5h-6.5a.75.75 0 0 1 0-1.5h2.5V3.5h-.984a.245.245 0 0 0-.124.033l-1.289.737c-.265.15-.564.23-.869.23h-.162l2.112 4.692a.75.75 0 0 1-.154.838l-.53-.53.529.531-.001.002-.002.002-.006.006-.016.015-.045.04c-.21.176-.441.327-.686.45C4.556 10.78 3.88 11 3 11a4.498 4.498 0 0 1-2.023-.454 3.544 3.544 0 0 1-.686-.45l-.045-.04-.016-.015-.006-.006-.004-.004v-.001a.75.75 0 0 1-.154-.838L2.178 4.5H1.75a.75.75 0 0 1 0-1.5h2.234a.249.249 0 0 0 .125-.033l1.288-.737c.265-.15.564-.23.869-.23h.984V.75a.75.75 0 0 1 1.5 0Zm2.945 8.477c.285.135.718.273 1.305.273s1.02-.138 1.305-.273L13 6.327Zm-10 0c.285.135.718.273 1.305.273s1.02-.138 1.305-.273L3 6.327Z"></path></svg>**MIT 许可证**。

![new2code/python-factorial 存储库主页的屏幕截图。 在右侧边栏中，“MIT 许可证”以深橙色框出。](/assets/images/help/repository/license-info-python-factorial.png)

我们希望复制整个 `factorial_finder.py` 文件，因此 MIT 许可证指示我们应该在自己的项目中添加许可证的副本。 在Python文件的顶部，将许可证粘贴为注释。

> \[!TIP] 可以使用[选择许可证](https://choosealicense.com/licenses/)工具了解其他常见许可证所允许的内容。

### 3.使用和修改代码片段

现在，你已准备好将代码片段粘贴到项目中。 虽然有时可以直接按原样使用代码片段，但经常需要根据自己的具体用例对其进行修改\*\*\*\*。 现在我们来练习一下吧！

假设我们想要快速计算 5、7、9 和 10 的阶乘。 我们可以把计算器放在一个以参数形式接收数字的函数中，而不是为每个数字复制粘贴整个程序\*\*\*\*。

使用 [Copilot Chat](https://github.com/copilot) 来建议和解释实现。 将当前代码粘贴到聊天窗口中，后接以下提示：

```text copy
Wrap the Python code above in a function.
```

Copilot 将生成如下所示的代码：

```python copy
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程序：

```python copy
print(calculate_factorial(5))
print(calculate_factorial(7))
print(calculate_factorial(9))
print(calculate_factorial(10))
```

祝贺你！ 你已成功找到、理解和修改了示例代码片段。

## 在项目中使用库中的代码

现在，我们来了解如何使用库，对开发人员而言，这是标准做法\*\*\*\*。 库本质上是其他开发人员编写的用于执行特定任务的代码集合。 可以将库导入到项目中以使用预先编写的代码，从而节省时间和精力。

在本部分中，我们将继续使用上一部分中的Python因子计算器示例。 下面是我们的当前代码，供你参考：

```python copy
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](https://github.com/copilot)，然后询问：

```text copy
Is there a Python library with a function for calculating a factorial?
```

Copilot 将告诉我们，标准 Python 库中的 [`math`](https://docs.python.org/3/library/math.html) 模块包含一个阶乘函数。

### 2.确定项目中安全性的优先级

向项目添加库或模块时，将创建所谓的依赖项\*\*\*\*。 依赖项是预先编写的代码包，项目依赖其来正确运行。 如果依赖项没有被仔细编写或维护，它们可能会给您的工作引入安全漏洞。

值得庆幸的是，可以采取一些措施来为项目提供最佳保护。 现在我们来练习一下。

#### 使用流行的软件库

热门库很有可能是安全的，因为许多开发人员都在积极维护和使用它们。 判断热门程度的一个明显标志是存储库拥有的**星星**。 如果找不到依赖项的 GitHub 存储库，可以寻求帮助 Copilot 。

打开 [Copilot Chat](https://github.com/copilot)，然后询问：

```text copy
Find the GitHub repository containing the code for the math module in Python.
```

Copilot 将告诉你 `math` 模块是在 [`python/cpython`](https://github.com/python/cpython) 中定义的，其中有超过 64,000 颗星。

#### 为项目启用 Dependabot alerts

启用后，当 Dependabot alerts 检测到依赖项中的安全问题时，Dependabot 会自动生成，从而帮助你快速修复漏洞。
Dependabot可在所有开放源代码\*\*\*\* 存储库上GitHub获取。

立即为存储库启用 Dependabot alerts 。 单击项目<svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-shield" aria-label="shield" role="img"><path d="M7.467.133a1.748 1.748 0 0 1 1.066 0l5.25 1.68A1.75 1.75 0 0 1 15 3.48V7c0 1.566-.32 3.182-1.303 4.682-.983 1.498-2.585 2.813-5.032 3.855a1.697 1.697 0 0 1-1.33 0c-2.447-1.042-4.049-2.357-5.032-3.855C1.32 10.182 1 8.566 1 7V3.48a1.75 1.75 0 0 1 1.217-1.667Zm.61 1.429a.25.25 0 0 0-.153 0l-5.25 1.68a.25.25 0 0 0-.174.238V7c0 1.358.275 2.666 1.057 3.86.784 1.194 2.121 2.34 4.366 3.297a.196.196 0 0 0 .154 0c2.245-.956 3.582-2.104 4.366-3.298C13.225 9.666 13.5 8.36 13.5 7V3.48a.251.251 0 0 0-.174-.237l-5.25-1.68ZM8.75 4.75v3a.75.75 0 0 1-1.5 0v-3a.75.75 0 0 1 1.5 0ZM9 10.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path></svg> Security and quality存储库的**GitHub** 选项卡。 在Dependabot alerts旁边，单击**启用 Dependabot alerts**。 可以从边栏的选项卡访问Dependabot alerts**Dependabot**。

### 3.从库实现代码

现在，你已准备好将库导入项目中，然后在代码中使用其内容。 你可以阅读该库的文档，学习如何自己完成这个任务，也可以请 Copilot 为你建议并解释一个实现方案。

打开 [Copilot Chat](https://github.com/copilot)，然后询问：

```text copy
How do I use the factorial function of the math module in my Python project?
```

Copilot 然后，将建议以下代码的版本：

```python copy
import math

# Calculate the factorial of a number
number = 5
result = math.factorial(number)

print(f"The factorial of {number} is {result}")
```

将项目中的现有代码替换为上述实现后，你已成功在示例项目中使用库中的代码！

## 共享你的工作

本教程介绍了如何在自己的工作中安全地重复使用其他人的代码。 若要展示你的成功，请在我们的[社区讨论](https://github.com/orgs/community/discussions/153140)中分享你是如何重新利用代码并基于示例项目进行构建的。

## 其他阅读材料

* [查找和了解示例代码](/zh/get-started/learning-to-code/finding-and-understanding-example-code)