學Python,怎能不懂點PEP?

PEP是Python增強提案(Python Enhancement Proposal)的縮寫。社區(qū)通過PEP來給Python語言建言獻策,每個版本的新特性和變化都是通過PEP提案經(jīng)過社區(qū)決策層討論、投票決議,最終確定的。

也就是說,PEP是各種增強功能和新特性的技術(shù)規(guī)格,也是社區(qū)指出問題、精確化技術(shù)文檔、推動Python發(fā)展的提案。一般情況下,可以將PEP視為Python語言的設(shè)計文檔,包含了技術(shù)規(guī)范和功能的基本原理說明等。

分類

官方將PEP分成三類:

  • I - Informational PEP:信息類,說明 Python 中的某一個設(shè)計問題,指導方針、共識,發(fā)布日程等內(nèi)容。例如PEP 20(The Zen of Python,即著名的Python之禪)、PEP 404 (Python 2.8 Un-release Schedule,即宣告不會有Python2.8版本)。

  • P - Process PEP:流程類,Python 本身之外的周邊信息,開發(fā)中使用的工具、流程或者環(huán)境的更改說明。PEP 1(PEP Purpose and Guidelines,即關(guān)于PEP的指南)、PEP 347(Migrating the Python CVS to Subversion,即關(guān)于遷移Python代碼倉)。

  • S - Standards Track PEP:標準類,數(shù)量最多的提案,描述Python的新功能和新實踐。

讀Python源代碼可以幫助你了解Python的當前的處理邏輯,讀PEP文檔可以幫助你了解Python設(shè)計的來龍去脈,了解Python有哪些特性,為什么沒有某些特性,Python與其它語言特性的差異,為什么要設(shè)計這些特性,是怎么設(shè)計的,怎樣更好地運用它們。

說到底,PEP是深入了解Python的途徑,是真正掌握Python語言的一把鑰匙,也是得心應(yīng)手使用Python的一本指南。

PEP閱讀建議

PEP從2000年開始到目前已經(jīng)發(fā)布了幾百個,所有PEP都存在一個github repository倉庫中,PEP索引及分類匯總在PEP 0 — Index of Python Enhancement Proposals (PEPs),其中有一些對我們理解和使用python非常重要,推薦大家一讀:

  • PEP 20 – The Zen of Python: 應(yīng)該始終踐行的python之禪,python命令行輸入“import this”。
The Zen of Python
  • PEP 8 – Style Guide for Python Code: Python代碼規(guī)范和應(yīng)該遵守的編碼原則,Python編碼風格指南,必讀。

  • PEP 257 – Docstring Conventions: 文檔注釋規(guī)范,提高代碼的可維護性。

  • PEP 287 – reStructuredText Docstring Format: 使用 reStructuredText 標記語言編寫帶格式的復雜文檔。

  • PEP 3333 — Python Web Server Gateway Interface v1.0.1: 更新版WSGI協(xié)議,告訴你如何在web服務(wù)器與web應(yīng)用/web框架之間交互的可移植標準接口。

Python 2

Python 2 版本重點關(guān)注以下PEP:

  • PEP 201 – Lockstep Iteration (zip function) (2.0)

  • PEP 202 – List Comprehensions (2.0) - 列表生成式

  • PEP 221 – Import As (2.0)

  • PEP 234 – Iterators (2.1) - 迭代器

  • PEP 236 – Back to the __future__ (2.1) - 回到未來,把下一個新版本的特性導入到當前版本

  • PEP 255 – Simple Generators (yield) (2.2) - 生成器

  • PEP 279 – The enumerate() built-in function (2.3) - 枚舉方法

  • PEP 282 – A Logging System (2.3) - 日志模塊

  • PEP 285 – Adding a bool type (2.3) - 布爾型

  • PEP 289 – Generator Expressions (2.4) - 生成器表達式

  • PEP 308 – Conditional Expressions (2.4) - Python 的三元式: X if C else Y

  • PEP 318 – Decorators for Functions and Methods (2.4) - 裝飾器

  • PEP 322 – Reverse Iteration (2.4) - 反向迭代

  • PEP 324 – subprocess - New process module (2.4)

  • PEP 327 – Decimal Data Type (2.4) - 小數(shù)點精確計算

  • PEP 341 – Unifying try-except and try-finally - try … except … else … finally ...

  • PEP 342 – Coroutines via Enhanced Generators (2.5) - 協(xié)程

  • PEP 343 – The “with” Statement (2.5) - with語句

  • PEP 367 – New Super (2.6) - super調(diào)用

Python 2 and 3

  • PEP 274 – Dict Comprehensions (originally 2.3, then 2.7 and 3.0) - 字典表達式

  • PEP 372 – Adding an ordered dictionary to collections (2.7, 3.1) - 有序字典

  • PEP 389 – argparse - New Command Line Parsing Module (2.7, 3.2) - 命令行參數(shù)解析模塊

Python 3

Python 3 重點:

  • PEP 380 – Syntax for Delegating to a Subgenerator (yield from) (3.3) - 引入“yield from”語法

  • PEP 405 – Python Virtual Environments (3.3) - 虛擬環(huán)境

  • PEP 435 – Adding an Enum type to the Python standard library (3.4) - 引入枚舉類型

  • PEP 484 - Type Hints (3.5) - 類型約束

  • PEP 485 – A Function for testing approximate equality (3.5) - 約等于

  • PEP 492 – Coroutines with async and await syntax (3.5) - 協(xié)程引入async/await語法

  • PEP 525 – Asynchronous Generators (3.6) - 異步生成器

  • PEP 526 – Syntax for Variable Annotations (3.6) - 變量聲明

  • PEP 530 – Asynchronous Comprehensions (3.6) - 異步表達式 asynchronous versions of list, set, dict comprehensions and generator expressions

  • PEP 3101 – Advanced String Formatting (3.0) - 新的字符串格式化方法

  • PEP 3105 – Make print a function (3.0) - 為什么Python3要把print改為函數(shù)

  • PEP 3107 – Function Annotations (3.0)

  • PEP 3115 – Metaclasses in Python 3000 (3.0)

  • PEP 3119 – Introducing Abstract Base Classes (3000) - 抽象基類

  • PEP 3120 – Using UTF-8 as the default source encoding (3.0)

  • PEP 3129 – Class Decorators (3.0)

  • PEP 3135 – New Super (3.0) - PEP 367 for Python 3.X

  • PEP 3148 – futures - execute computations asynchronously (3.2) - futures的詳細介紹

  • PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module (3.3) - 異步IO

參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容