- Mastering Python
- Rick van Hattem
- 442字
- 2021-07-16 11:10:32
Chapter 2. Pythonic Syntax, Common Pitfalls, and Style Guide
The design and development of the Python programming language have always been in the hands of its original author, Guido van Rossum, in many cases lovingly referred to as the Benevolent Dictator For Life (BDFL). Even though van Rossum is thought to have a time machine (he has repeatedly answered feature requests with "I just implemented that last night": http://www.catb.org/jargon/html/G/Guido.html), he is still just a human and needs help with the maintenance and development of Python. To facilitate that, the Python Enhancement Proposal (PEP) process has been developed. This process allows anyone to submit a PEP with a technical specification of the feature and a rationale to defend its usefulness. After a discussion on the Python mailing lists and possibly some improvements, the BDFL will make a decision to accept or reject the proposal.
The Python style guide (PEP 8
: https://www.python.org/dev/peps/pep-0008/) was once submitted as one of those PEPs, and it is has been accepted and improved regularly since. It has a lot of great and widely accepted conventions as well as a few disputed ones. Especially, the maximum line length of 79 characters is a topic of many discussions. Limiting a line to 79 characters does have some merits, however. In addition to this, while just the style guide itself does not make code Pythonic, as "The Zen of Python" (PEP 20
: https://www.python.org/dev/peps/pep-0020/) elegantly says: "Beautiful is better than ugly." PEP 8
defines how code should be formatted in an exact way, and PEP 20
is more of a philosophy and mindset.
The common pitfalls are a list of common mistakes made, varying from beginner mistakes to advanced ones. They range from passing a list or dictionary (which are mutable) as arguments to late-binding problems in closures. An even more important issue is how to work around circular imports in a clean way.
Some of the techniques used in the examples in this chapter might be a bit too advanced for such an early chapter, but please don't worry. This chapter is about style and common pitfalls. The inner workings of the techniques used will be covered in later chapters.
We will cover the following topics in this chapter:
- Code style (
PEP 8
,pyflakes
,flake8
, and more) - Common pitfalls (lists as function arguments, pass by value versus pass by reference, and inheritance behavior)
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- HornetQ Messaging Developer’s Guide
- C語言程序設計(第2 版)
- LabVIEW入門與實戰開發100例
- 零基礎玩轉區塊鏈
- Scala Design Patterns
- Monitoring Elasticsearch
- Hands-On Microservices with Kotlin
- C語言實驗指導及習題解析
- Android底層接口與驅動開發技術詳解
- Mastering JavaScript High Performance
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- 零基礎Java學習筆記
- 實戰Java高并發程序設計(第2版)
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計