Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

上手 Python 数据类前需要知道的 6 件事 #7753

Conversation

JalanJiang
Copy link
Contributor

译文翻译完成,resolve #7648

@jiapengwen
Copy link

@Glowin 认领校对

@lsvih
Copy link
Member

lsvih commented Dec 11, 2020

@jiapengwen 欢迎校对~


Data are the most essential element in any programming project. All programs are destined to have certain interactions with data. For instance, if your project is a website, you have to present data (e.g., texts and images) in a user-friendly manner. If your project uses machine learning to predict financial trends, you have to prepare data that can be learned by your models.
在任何程序项目中,数据都是最重要的元素。所有的程序都必然会与数据进行某些互动。例如,你的项目是一个 Web 站点,你就需要以一个用户友好的方式展现数据(文本或图片)。如果你的项目使用机器学习来预测经济趋势,你就需要用模型可以学习的方式来准备数据。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你就需要用模型可以学习的方式来准备数据 -> 你就需要准备数据用来被你的模型学习


However, data mutability isn’t always desired. In some cases, you don’t want your data to be mutable. In such a scenario, you can consider specifying the `frozen` parameter to be `True` when you use the `dataclass` decorator. A trivial example is shown below.
然而,数据可变性并非总是理想的。在一些场景中,你不希望数据可以被改变。在这种情况下,可以考虑在使用 `dataclass` 装饰器时将 `frozen` 参数设置为 `True`。下面是一个简单的例子:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数据可变性并非总是理想的 -> 数据可变性并非总是想要的

@@ -205,19 +205,19 @@ Bill 1 costs: [5, 7]
True
```

The above code shows you that when your function uses a mutable default value, you can’t really set the default value using a mutable instance because this object will be shared by all callers who don’t specify the parameter. In the above example, as you can see, both instances are sharing the same underlying list object, which causes undesired side effects.
由上面的代码可知,当你的函数使用了可变默认值时,你实际上并不能给可变实例设置默认值,因为这个可变对象被所有未指定参数的调用者共享。在上面的示例中,如你所见,两个实例共享底层的列表对象,这导致了我们不愿看到的结果。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由上面的代码可知 -> 上面的代码展示了,

@chzh9311
Copy link
Contributor

@lsvih 校对认领

@lsvih
Copy link
Member

lsvih commented Jan 17, 2021

@chzh9311 好的~

Copy link
Contributor

@chzh9311 chzh9311 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsvih @JalanJiang 校对完成~


Data-class-related functionalities are defined in the `dataclasses` module, which is part of the standard Python library. The most important element of this module is the `dataclass` decorator. The following code shows you how we use the `dataclass` decorator for our custom class that we define to manage bills in a restaurant.
Python 的标准库 `dataclasses` 模块定义了数据与类关联的功能。这个模块最重要的元素是 `dataclass` 装饰器。下面的代码向你展示了如何使用 `dataclass` 装饰器来定义一个用于管理餐厅账单的自定义类。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『Python 的标准库 dataclasses 模块定义了数据与类关联的功能。』=>『Python 标准库中的 dataclasses 模块定义了数据类相关的功能』

@lsvih lsvih added enhancement 等待译者修改 and removed 正在校对 labels Feb 3, 2021
@lsvih
Copy link
Member

lsvih commented Feb 3, 2021

@JalanJiang 有空来看看校对哈-。-

@PassionPenguin
Copy link
Contributor

校对认领:)@lsvih

@lsvih
Copy link
Member

lsvih commented Feb 3, 2021

@PassionPenguin 这篇校对已经够了,现在等译者修改。。

@JalanJiang
Copy link
Contributor Author

@lsvih 已根据校对意见修改

@lsvih
Copy link
Member

lsvih commented Feb 8, 2021

我添加了校对者信息,您记得同步一下哈

@lsvih lsvih merged commit b845848 into xitu:master Feb 8, 2021
@lsvih
Copy link
Member

lsvih commented Feb 8, 2021

@JalanJiang 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。

掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件
专栏地址:https://zhuanlan.zhihu.com/juejinfanyi

@lsvih lsvih added 翻译完成 and removed enhancement 等待译者修改 labels Feb 8, 2021
@JalanJiang
Copy link
Contributor Author

我添加了校对者信息,您记得同步一下哈

忘记填写了,抱歉😭

@JalanJiang
Copy link
Contributor Author

@JalanJiang 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。

掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件
专栏地址:https://zhuanlan.zhihu.com/juejinfanyi

https://juejin.cn/post/6926815217859559438/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

上手 Python 数据类前需要知道的 6 件事
5 participants