重点 (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


http://www.taodudu.cc/news/show-994799.html

相关文章:

  • 数据探查_数据科学家,开始使用探查器
  • 从ncbi下载数据_如何从NCBI下载所有细菌组件
  • 线性插值插值_揭秘插值搜索
  • 如果您不将Docker用于数据科学项目,那么您将生活在1985年
  • docker部署flask_使用Docker,GCP Cloud Run和Flask部署Scikit-Learn NLP模型
  • 问卷 假设检验 t检验_真实问题的假设检验
  • 大数据技术 学习之旅_为什么聚焦是您数据科学之旅的关键
  • 无监督学习 k-means_无监督学习-第4部分
  • 深度学习算法原理_用于对象检测的深度学习算法的基本原理
  • 软件本地化 pdf_软件本地化与标准翻译
  • 数据库不停机导数据方案_如何计算数据停机成本
  • python初学者_面向初学者的20种重要的Python技巧
  • 贝叶斯网络建模
  • 数据科学家数据分析师_使您的分析师和数据科学家在数据处理方面保持一致
  • python db2查询_如何将DB2查询转换为python脚本
  • 爱因斯坦提出的逻辑性问题_提出正确问题的重要性
  • 餐厅数据分析报告_如何使用数据科学选择理想的餐厅设计场所
  • 熊猫直播 使用什么sdk_没什么可花的-但是16项基本操作才能让您开始使用熊猫
  • 关系型数据库的核心单元是_核中的数据关系
  • 小程序 国际化_在国际化您的应用程序时忘记的一件事
  • robo 3t连接_使用robo 3t studio 3t连接到地图集
  • 软件需求规格说明书通用模版_通用需求挑战和机遇
  • 一类动词二类动词三类动词_基于http动词的完全无效授权技术
  • 一年了
  • 将DataSet中的操作更新到Access数据库
  • 我喜欢的一首歌--《幸福的瞬间》
  • XForum 里用 Filter 编程实现安全访问控制
  • chedandekaoyan
  • Microsoft好员工的十个标准
  • GARFIELD@11-20-2004

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

  1. python需要下载哪些软件-80%的人都不知道,全球Python库下载前10名

    题图漫威宇宙英雄 Python的简洁性,不仅仅在于其语法简单,还有各种python库函数的支持,为大家节省了大量的时间和精力,所以网上有人戏称python的编程者为调包侠.但是你知道全球最受欢迎的py ...

  2. 很多人都不知道的小秘密,c++中拷贝分为两种

    深拷贝与浅拷贝 下面为作者的总结以深刻理解深拷贝和甜拷贝 深拷贝:如果创建类对象时,相应的构造函数内用运算符new为对象的数据成员申请内存空间,那么在析构函数中应该用运算符 delete释放相应的内存 ...

  3. 为什么在Python代码中需要装饰器

    Python is praised for its clarity and syntactic sugariness. In this article, I will teach you to use ...

  4. android9有深色模式吗,深色模式还是黑色模式?微信把所有人都搞懵了

    原标题:深色模式还是黑色模式?微信把所有人都搞懵了 前一阵子,微信正式加入了对"深色模式"的支持,这也是除了Windows Phone 版本以外微信第一次从系统层面支持深色模式.虽 ...

  5. arcgis里python窗口运行,在 Python 窗口中执行工具

    当第一次打开 Python 窗口时,它会显示类似这样的界面: 左侧区域为 Python 的主提示窗口,在这里执行 Python 命令.右侧区域为帮助和语法窗口,工具运行时,在这里显示执行消息:输入代码 ...

  6. python的运行窗口-在 Python 窗口中执行工具

    当第一次打开 Python 窗口时,它会显示类似这样的界面: 左侧区域为 Python 的主提示窗口,在这里执行 Python 命令.右侧区域为帮助和语法窗口,工具运行时,在这里显示执行消息:输入代码 ...

  7. python interpreter 中没有torch_PyTorch扩展自定义PyThon/C++(CUDA)算子的若干方法总结

    在做毕设的时候需要实现一个PyTorch原生代码中没有的并行算子,所以用到了这部分的知识,再不总结就要忘光了= =,本文内容主要是PyTorch的官方教程的各种传送门,这些官方教程写的都很好,以后就可 ...

  8. python 类中定义列表_Python-从类定义中的列表理解访问类变量

    小编典典 类范围和列表,集合或字典的理解以及生成器表达式不混合. 为什么:或者,官方用词 在Python 3中,为列表理解赋予了它们自己的适当范围(本地名称空间),以防止其局部变量渗入周围的范围内(即 ...

  9. 一道Python面试题,据说大部分人都中招了,纷纷开始怀疑自己

    无意间,看到这么一道Py无意间,看到这么一道Python面试题:以下代码将输出什么? def testFun(): temp = [lambda x : i*x for i in range(4)] ...

最新文章

  1. 把一个数组的值存入二叉树中,然后利用前序、中序、后序3种方式进行遍历(完整代码以及运行结果)(Java)
  2. 走在网页游戏开发的路上(十)
  3. echarts中graphic_Echarts实现折线图
  4. oracle中的存储过程
  5. 古风素材无水印免费下载
  6. JAVA核酸预约检测管理系统毕业设计 开题报告
  7. 时间与空间的相对性——思想实验推导狭义相对论(四)
  8. 全球前沿技术趋势报告;华为发布Mate 40/Pro 系列新机;Windows 计算器移植到到 Linux...
  9. Codeforces 949A Zebras(构造)
  10. [DEMO] 互联网广告RTB机制简介
  11. 论文阅读《“The Boating Store Had Its Best Sail Ever”: Pronunciation-attentive ....》
  12. ESXI下安装OpenWrt/LEDE软路由教程(附超全功能固件镜像下载)
  13. 多边形(polygon)
  14. win10 彻底卸载docker
  15. Android多人视频聊天应用的开发(一)快速集成
  16. 工程流体力学笔记1(质点导数的公式与定义)
  17. YTU 3090 团体操排序
  18. 疯狂的XRP:3招击败ETH,或在2019年持续爆发
  19. 最少钱币数不java,【动态规划专题】3:换钱的最少货币数
  20. [Error] 'else' without a previous 'if'

热门文章

  1. android linux网络连接,Android和Linux服务器之间的TCP连接
  2. 再谈二叉树(二叉树概念,二叉树的性质,二叉树的存储结构)
  3. 剑指Offer09. 用两个栈实现队列
  4. 信号量释放和等待函数sem_post()和sem_wait()
  5. 【汇编语言】程序设计过程,如何避免数据类型匹配错误?
  6. Makefile(二)
  7. cs硕士妹子找工作经历【阿里人搜等互联网】
  8. hdu_2048 错排问题
  9. 洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)
  10. 搭建Maven私服那点事