每个人都应该使用的Python 3中被忽略的3个功能

重点 (Top highlight)

Python 3 has been around for a while now, and most developers — especially those picking up programming for the first time — are already using it. But while plenty of new features came out with Python 3, it seems like a lot of them are unknown or underutilized. In today’s article, I’ll talk about three lesser-known yet very useful features. They are features that I’ve come to know and love in other languages that I really think make Python 3 great.

Python 3已经存在了一段时间,并且大多数开发人员(尤其是那些初次接触程序的开发人员)已经在使用它。 但是,尽管Python 3推出了许多新功能,但似乎其中许多功能还是未知的或未得到充分利用。 在今天的文章中,我将讨论三个鲜为人知但非常有用的功能。 它们是我真正认为使Python 3很棒的其他语言所熟悉和喜爱的功能。

枚举 (Enumerations)

Enums are something I’ve used in Java and Swift a lot, and my usage of them extends into Python.

枚举是我在Java和Swift中经常使用的东西,我对它们的使用扩展到了Python中。

Declaring an enum in Python is very easy and could also be done prior to Python 3 (albeit with more limited functionality):

在Python中声明一个枚举非常容易,也可以在Python 3之前完成(尽管功能有限):

In the code above, you can see an enum is easily declared by declaring a class and making it a subclass of Enum. From there, you just define each of your states in the following lines.

在上面的代码中,可以看到通过声明一个类并将其作为Enum的子类来轻松声明一个Enum 。 从那里,您只需在以下几行中定义每个状态。

In my case, I had AIR, LAND, and SEA available.

就我而言,我有AIRLANDSEA可用。

The functionality that was added in Python 3 is the ability to do .value and .name. Those will allow you to get the associated integer value with the state or the string associated with it.

Python 3中添加的功能是可以执行.value.name 。 这些将允许您获取带有状态或与之关联的字符串的关联整数值。

In the code above, printing State.LAND.name will return LAND, so the functionality is more than just an integer.

在上面的代码中,打印State.LAND.name将返回LAND ,因此功能不仅限于整数。

Enums are useful in code when you want a descriptive representation of constants. For example, instead of checking if a state is 0 or 1, it is much better to check if it is State.MOVING or State.STATIONARY. Your constants could change, and if someone is looking at your code, MOVING makes a lot more sense than 0. As a result, readability is greatly improved.

当您想要常量的描述性表示形式时,枚举在代码中很有用。 例如,与其检查状态是否为01 ,不如检查状态为State.MOVINGState.STATIONARY 。 您的常数可能会更改,并且如果有人在看您的代码,则MOVING0有意义得多。 结果,大大提高了可读性。

For more reading, check out the official Python 3 documentation on Enum here.

有关更多信息,请在此处查看Enum上的Python 3官方文档。

格式 (Format)

Added in Python 3.6, fstrings are a great way to format text. They provide much greater readability and are less error-prone (which I certainly enjoy, coming from languages like Java).

fstrings是在Python 3.6中添加的,是格式化文本的好方法。 它们提供了更高的可读性,并且不易出错(我当然很喜欢,来自Java之类的语言)。

fstrings are a more readable way than the format previously used in Python. Here is an example of using format:

fstrings比以前在Python中使用的format更易读。 这是使用format的示例:

As you can see we have empty brackets through the string and then afterwards we list out the name of each variable in order.

如您所见,我们在字符串中使用了方括号,然后按顺序列出了每个变量的名称。

Now take a lot at the same code but using fstring it is much more readable, and very akin to formatting a string in Swift.

现在花很多时间在相同的代码上,但是使用fstring更具可读性,非常类似于在Swift中格式化字符串。

To accomplish this cleaner string, we simply preface our quotes with the letter f and then instead of having empty brackets, we put the variable or data into the brackets directly. Since the variables are written within the brackets themselves you don’t have to count the number of items written in format to figure out what variable is placed where — the variables exists right where they are going to be placed.

为了完成此更清晰的字符串,我们只需在引号前加上字母f ,然后将变量或数据直接放在方括号中即可,而不用使用空括号。 由于变量是用括号括起来的,因此您不必计算以格式编写的项目数,就可以知道将哪个变量放置在什么位置-变量存在于要放置的位置。

Doing fstrings produces much more readable and reliable code than doing something like string concatenation or format strings.

与执行字符串连接或格式化字符串之类的操作相比,执行fstrings产生的代码更具可读性和可靠性。

资料类别 (Data Classes)

Data classes may be a more obscure subject than the other topics I’ve touched on, so I’ll explain them briefly. Data classes are something I’ve grown to really like in Kotlin, so I really like trying to use them in Python as well.

数据类可能是一个比我提到的其他主题更晦涩的主题,因此我将简要解释它们。 数据类是我在Kotlin中逐渐喜欢的东西,因此我也非常想尝试在Python中使用它们。

A data class is effectively a class whose sole purpose is to literally hold data. The class will have variables that can be accessed and written to, but there is no extra logic on top of it.

数据类实际上是一个类,其唯一目的是从字面上保留数据。 该类将具有可以访问和写入的变量,但是它之上没有多余的逻辑。

Imagine you have a program and you pass a string and an array of numbers between different classes. You could just have methods like pass(str, arr), but a much better approach would be to make a data class that only contains a string as a field and an array.

假设您有一个程序,并且在不同的类之间传递了一个字符串和一个数字数组。 您可能只具有pass(str, arr) ,但是更好的方法是制作一个仅包含字符串作为字段和数组的数据类。

By making a data class, what you are doing will be much clearer and it will also be easier to unit test.

通过创建数据类,您所做的事情将更加清楚,并且单元测试也将更加容易。

I’ll give an example of how to make a simple data class that represents a three-dimensional vector, but this can easily be extended to represent any combination of different data:

我将给出一个示例,说明如何制作表示三维向量的简单数据类,但这可以轻松扩展以表示不同数据的任何组合:

Here, you can see the definition of a data class is very similar to declaring a normal class, except we use @dataclass before it and then each field is declared like name: type.

在这里,您可以看到数据类的定义与声明普通类非常相似,不同之处@dataclass我们在数据类之前使用@dataclass ,然后每个字段都像name: type一样被声明。

While the functionality of our created Vector3D is limited, the point of the data class is just to increase efficiency and reduce errors in your code. It’s much better to pass around a Vector3D than int variables.

虽然我们创建的Vector3D的功能受到限制,但数据类的目的只是为了提高效率并减少代码中的错误。 这是更好的通过周围Vector3Dint变量。

For more detail on @dataclass check out the official Python 3 documentation here.

欲了解更多细节上@dataclass查看官方的Python 3文档在这里

结论 (Conclusion)

If you’ve tried any of these new features let me know in a comment! I’d love to hear your different use cases for them. Happy coding!

如果您尝试过任何这些新功能,请在评论中告诉我! 我很想听听您的不同用例。 编码愉快!

翻译自: https://medium.com/better-programming/3-neglected-features-in-python-3-that-everyone-should-be-using-65cffc96f235

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值