每个人都应该使用的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.
就我而言,我有AIR , LAND和SEA可用。
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.
當您想要常量的描述性表示形式時,枚舉在代碼中很有用。 例如,與其檢查狀態是否為0或1 ,不如檢查狀態為State.MOVING或State.STATIONARY 。 您的常數可能會更改,并且如果有人在看您的代碼,則MOVING比0有意義得多。 結果,大大提高了可讀性。
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的功能受到限制,但數據類的目的只是為了提高效率并減少代碼中的錯誤。 這是更好的通過周圍Vector3D比int變量。
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
總結
以上是生活随笔為你收集整理的每个人都应该使用的Python 3中被忽略的3个功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到什么最好
- 下一篇: python初学者_面向初学者的20种重